POV-Ray : Newsgroups : povray.general : large numbers turn random negative!? Server Time
4 Dec 2024 21:37:39 EST (-0500)
  large numbers turn random negative!? (Message 1 to 4 of 4)  
From: Leroy
Subject: large numbers turn random negative!?
Date: 24 Jul 2023 14:30:00
Message: <web.64bec2382d0df701b457f86cf712fc00@news.povray.org>
I have pov3.7 on my winXP.
While playing around with making music using POV. I made long rhythm strings
and turn them into numbers and visa versa. Using random numbers to check things
out, when negative random numbers show up. With NO negative numbers on the page.
It's not a big deal in my case. But I wonder if this is a window thing, or a
float thing in POV,and if anyone else came a cross this.

Here's the test code the should show the problem:

#declare Cm=pow(2,40); #debug concat("Cm =",str(Cm,0,0),"\n")
#declare Cv=Cm-1;       #debug concat("Cv =",str(Cv,0,0),"\n")
#declare S=seed(423);
#declare Mv=int(rand(S)*Cv); #debug concat("Mv =",str(Mv,0,0),"\n")
text{ttf "arial.ttf" concat("Mv=",str(Mv,0,0)) 0.4,0 scale 1 translate
<-4,1.5,0> pigment{rgb 1}}

 light_source{<0,0,-20>  rgb 1}
camera {
  orthographic
  location <0,0,-10>    // position & direction of view
  look_at  <0,0,0>
  right 10*x            // horizontal size of view
  up 10*y               // vertical size of view
}

Have Fun!


Post a reply to this message

From: jr
Subject: Re: large numbers turn random negative!?
Date: 24 Jul 2023 15:55:00
Message: <web.64bed6e6a82368980c03e9d6cde94f1@news.povray.org>
hi,

"Leroy" <whe### [at] gmailcom> wrote:
> I have pov3.7 on my winXP.
> While playing around with making music using POV.

as you do.. :-)


> I made long rhythm strings
> and turn them into numbers and visa versa. Using random numbers to check things
> out, when negative random numbers show up. With NO negative numbers on the page.
> It's not a big deal in my case. But I wonder if this is a window thing, or a
> float thing in POV,and if anyone else came a cross this.
>
> Here's the test code the should show the problem:
> ...

not a Windows thing, same (-2147483648) on Linux.  think it's a signed 32 bit
int related problem, as that number is half of the unsigned max.


regards, jr.


Post a reply to this message

From: kurtz le pirate
Subject: Re: large numbers turn random negative!?
Date: 25 Jul 2023 04:32:14
Message: <64bf888e$1@news.povray.org>
On 24/07/2023 20:27, Leroy wrote:
> I have pov3.7 on my winXP.
> While playing around with making music using POV. I made long rhythm strings
> and turn them into numbers and visa versa. Using random numbers to check things
> out, when negative random numbers show up. With NO negative numbers on the page.
> It's not a big deal in my case. But I wonder if this is a window thing, or a
> float thing in POV,and if anyone else came a cross this.

Window thing : no.
Float thing in POV : yes and no !

In fact, as is often the case, we come back to the
interpretation/implementation of the int() function
with binaries representation of floating point numbers
in 32 or 64 bits.


Try this piece of code :

#declare r = rand(S);
#debug concat("rand(S)           = ",str(r,0,-1),"\n")
#declare Mv=r*Cv;
#debug concat("rand(S)*Cv        = ",str(Mv,0,-1),"\n")
#declare Mv1 = int(Mv);
#debug concat("int(rand(S)*Cv)   = ",str(Mv1,0,0),"\n")
#declare Mv2 = floor(Mv);
#debug concat("floor(rand(S)*Cv) = ",str(Mv2,0,0),"\n")

And the results are :

rand(S)           = 0.501773
rand(S)*Cv        = 551705517183.952148
int(rand(S)*Cv)   = -2147483648
floor(rand(S)*Cv) = 551705517183



Have fun too with https://baseconvert.com/ieee-754-floating-point



-- 
Kurtz le pirate
Compagnie de la Banquise


Post a reply to this message

From: Leroy
Subject: Re: large numbers turn random negative!?
Date: 25 Jul 2023 14:00:00
Message: <web.64c00c9ea823689b457f86cf712fc00@news.povray.org>
kurtz le pirate <kur### [at] gmailcom> wrote:

> Window thing : no.
> Float thing in POV : yes and no !
>
> In fact, as is often the case, we come back to the
> interpretation/implementation of the int() function
> with binaries representation of floating point numbers
> in 32 or 64 bits.
>
>
> Try this piece of code :
>
> #declare r = rand(S);
> #debug concat("rand(S)           = ",str(r,0,-1),"\n")
> #declare Mv=r*Cv;
> #debug concat("rand(S)*Cv        = ",str(Mv,0,-1),"\n")
> #declare Mv1 = int(Mv);
> #debug concat("int(rand(S)*Cv)   = ",str(Mv1,0,0),"\n")
> #declare Mv2 = floor(Mv);
> #debug concat("floor(rand(S)*Cv) = ",str(Mv2,0,0),"\n")
>
> And the results are :
>
> rand(S)           = 0.501773
> rand(S)*Cv        = 551705517183.952148
> int(rand(S)*Cv)   = -2147483648
> floor(rand(S)*Cv) = 551705517183
>
>
>
> Have fun too with https://baseconvert.com/ieee-754-floating-point
>

Yep, the 'floor' thing works as expected.
I got use to use 'int' for every thing & don't use 'floor' or 'ceil' hardly at
all.(I had to look up 'ceil' to place it here)

Awhile back I was hacking some MTS video files. So I checked our how floats
where stored. It's a bit complicated, but the main thing I remembered is that it
starts with a sign bit. I just couldn't see how that got changed. Now I can see
a way. Dealing with C++ I've had some problems with signed and unsigned
integers.
Thanks for your fix I'll have to remember it.
Have fun!!


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.