VEX - frac() function - Floating Point Precision

   152   1   0
User Avatar
Member
2163 posts
Joined: Sept. 2015
Offline
Curiosity Post.

Occasionally here on the forums there's posts that come across matters that deal with inherent floating point precision variances.

Like this recent post:

https://www.sidefx.com/forum/topic/102733/ [www.sidefx.com]

I've been using the following snippet of vex for a while now but with no issues(works as intended in all cases so far):

int   Integer_Value_A = 0; // Replace 0 with Target Integer Attribute Min Value 
int   Integer_Value_B = 1; // replace 1 with Target Integer Attribute Max Value 
float Mid_Float_Value;
float Fractional_Value;

Mid_Float_Value  = (Integer_Value_A + Integer_Value_B) / 2.0;

Fractional_Value = frac(Mid_Float_Value);
f@AA = Fractional_Value;

if(frac(Fractional_Value) != 0.0) warning('Your mid target value is not a integer value');
//if(frac(Fractional_Value) != 0) warning('Your mid target value is not a integer value');// This works too

I would think though, because of possible floating point precision variances that my code would fail to function as intended because of how I am doing the comparisons.

But this is not the case; So far many times I am always getting the desired functionality.

My question of curiosity is how is the frac() function working?

Why am I NOT running into floating point precision issues?

Is the function itself doing an intentional type of 'truncating' of its return value?

Or does it inherently operation in 64 bit values and return 32 bit values by default(which gives an automatic truncation; I assume).

I would tend to think the 64 to 32 idea is not valid, because I tried my code with an attribute caste to 64 bit before trying my code - It still seems to work.

So I think the frac() function does an intentional truncation(rounding)? (regardless of whether in 32 or 64 bit mode).

Or I am missing something completely different?
Edited by BabaJ - Dec. 22, 2025 11:44:08
User Avatar
Member
9379 posts
Joined: July 2007
Offline
not missing anything, you still get fp errors
for example use these 2 numbers:

int   Integer_Value_A = 10000001; 
int   Integer_Value_B = 10000002;

the integer sum is 20000003, which is clearly not divisible by 2.0
however the fp32 sum is 2.0000004e7 and your code will return half as 1.0000002e7 with frac == 0
if you do it in fp64 it will be able to represent it correctly as 2.0000003e7 with half of 1.00000015e7 and frac == 0.5

you likely haven't run into this as you may have used low enough integer numbers that fit into mantissa of fp32
Tomas Slancik
CG Supervisor
Framestore, NY
  • Quick Links