Please tell me how a 'mod' function can be written for two floating point numbers.
there is a 'modf' function:
modf( f_flonum1f_flonum2 ) => f_result
Returns the floating-point remainder of the division of f_flonum1 by f_flonum2.
I'm facing another issue when using this function.
Why is it that the function, when executed directly in ICFB returns one value, and when executed inside a SKILL script, returns a different value?
I rather doubt that it's really doing this (I can't see how it would be possible). It's either that you're not seeing all the precision when numbers are displayed in the CIW (try doing sstatus(fullPrecision t) to change the default print representation - that doesn't affect the accuracy of the results, just how they're printed), or that the arguments you're providing to the function are not exactly the same (to full precision).
Can you give an example which illustrates this problem?
I can give an example where the function 'modf' is not giving the expected result.
I have two numbers 296.64 and 0.08, and 296.64 is perfectly divisible by 0.08 yielding a zero remainder.
But, I don't get 'zero' as result, when I use 'modf'.
Please find a screenshot of the function execution below...
Due to floating point rounding errors, the result is actually very slightly smaller than 0.08. This is because it's very hard to exactly represent decimal floating point numbers exactly in IEEE double precision floating point representation. So if you do:
you get -1.981748e-14
Or, put another way:
> sstatus(fullPrecision t)t> modf(296.64 0.08)0.07999999999998018
The same thing happens if you were to write a C program using the fmod() function. You probably need to do some amount of small rounding to correct for such potential rounding errors. Alternatively if these are representing coordinates, you could convert to integer (say, multiply by 1000 if the DBUperUU is 1000) and then use mod before dividing by 1000 again:
(I'm assuming the 80 is a fixed number).