Old 12-11-2018, 09:47 PM   #1
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 3,409
Default Is this a floating point thing?

Considering this:

Code:
slider1:25<0,100,1>

@slider

var_a = (slider1/1000)*srate+.5;        //debugger shows 1103
var_b = floor((slider1/1000)*srate+.5); //debugger shows 1102
var_c = floor((25/1000)*srate+.5);      //debugger shows 1103
Should I assume this is a floating point math thing? I.e. the 25 coming from the slider is infinitesimally smaller so the "1103" the debugger shows is actually just shy, hence the floor() to 1102?

Last edited by clepsydrae; 12-12-2018 at 04:48 PM.
clepsydrae is offline   Reply With Quote
Old 12-12-2018, 04:57 AM   #2
geraintluff
Human being with feelings
 
geraintluff's Avatar
 
Join Date: Nov 2009
Location: mostly inside my own head
Posts: 349
Default

Yes, that's a floating-point thing. Dividing by any number apart from a power of 2 is going to be a little bit approximate.

The debugger rounds the values for display - you can get a more detailed output using @gfx:

Code:
@gfx

gfx_x = gfx_y = 10;
gfx_r = gfx_g = gfx_b = 1;
gfx_printf("var_a: %.15f\nvar_b: %.15f\nvar_c: %.15f", var_a, var_b, var_c);


You need 15 decimal places of display to see that, though.

This means that if (as in your example) you round to the nearest integer using "floor(x + 0.5)", then values of x exactly halfway between two integers can go either way, depending what calculations you took to get there.
Attached Images
File Type: png gfx_output.png (1.2 KB, 269 views)
__________________
JSFX set | Bandcamp/SoundCloud/Spotify

Last edited by geraintluff; 12-12-2018 at 08:35 AM.
geraintluff is offline   Reply With Quote
Old 12-12-2018, 08:10 AM   #3
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,793
Default

That is why in eel a compare with "a == b" is done with a reasonable approximation, while to do an exact compare you do "a === b".

The short way of forcing (not too large) integer is "a |= 0;"

-Michael
mschnell is online now   Reply With Quote
Old 12-12-2018, 08:21 AM   #4
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,746
Default

I'm guessing that the reason this differs:

Code:
floor((25/1000)*srate+.5)
Is because 25/1000 is pre-calculated and stored in 64-bits, vs
Code:
floor((slider1/1000)*srate+.5);
slider1/1000 preserves the full results (80 bits in the FPU), then multiplies it by srate.

To test this theory, you can either rewrite it as:
Code:
floor((slider1*srate)/1000+.5);
Or you can try adding this near the top of your script (not recommended for production code though!)
Code:
//#eel-no-optimize:1
(This will remove all kinds of optimizations including value pre-calculation).
Justin is offline   Reply With Quote
Old 12-12-2018, 04:45 PM   #5
clepsydrae
Human being with feelings
 
clepsydrae's Avatar
 
Join Date: Nov 2011
Posts: 3,409
Default

Quote:
Originally Posted by Justin View Post
I'm guessing that the reason this differs:
Thanks! -- reordering the computation confirmed your theory. //#eel-no-optimize:1 had no effect, though.
clepsydrae is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.