Hey,
Please explain this:
Mod(10,1) -> 0
Mod(1.0,0.1) -> 0.1
It seems like a bug to me...
Cheers!
Mod() using floats
Started by
JambaFun
, Sep 20 2007 09:44 AM
8 replies to this topic
#1
-
Posted 20 September 2007 - 09:44 AM
Tsss...
No can do since I want the floating point remainder.
No can do since I want the floating point remainder.
#3
-
Posted 20 September 2007 - 09:58 AM
What floating point remainder?
No remainder...
Btw, you can redefine built-in functions.
Mod(Dividend, Divisor): Modulo. Returns the remainder when Dividend is divided by Divisor.
Mod(1.0,0.1)1.0 / 0.1 = 10
No remainder...
Btw, you can redefine built-in functions.
mod(a, b) { return a - b * floor(a/b) }Obviously not as efficient as what AHK uses (qmathFmod(); 6 assembly instructions :shock:.)
#4
-
Posted 20 September 2007 - 12:58 PM
Well, zero of course.What floating point remainder?
Mod(1.0,0.1) incorrectly returns 0.1.
#5
-
Posted 20 September 2007 - 04:02 PM
This could be the old inexact floating point representation problem. Short decimal numbers have often infinite, periodic binary representations, which have to be truncated internally. Consequently, the results of arithmetic operations are also just approximate.
SetFormat Float, 0.20 MsgBox % Mod(1.0,0.1) ; 0.09999999999999995000There is no bug here. You cannot always expect exact floating point results.
#6
-
Posted 20 September 2007 - 07:46 PM
thanks for the solution Lexikos...worked great for what i needed
#7
-
Posted 17 August 2009 - 08:51 PM
Strange. I don't know what to say about this.
Clearly, Mod(10,1) = 0, so Mod(1.0,0.1) should also result in 0. And yet, 3 other languages I've tested this in returns Mod(1.0,0.1) = 0.1, while most calculators return 0.
Consider Mod(0.3,0.2) should be 0.1, no? And that's what I get in those languages that support non-integer values, but Mod(1.0,0.1) just doesn't make sense for it to return 0.1. It's a mystery to me.
I'm going to ask in some #math IRC channels.
Clearly, Mod(10,1) = 0, so Mod(1.0,0.1) should also result in 0. And yet, 3 other languages I've tested this in returns Mod(1.0,0.1) = 0.1, while most calculators return 0.
Consider Mod(0.3,0.2) should be 0.1, no? And that's what I get in those languages that support non-integer values, but Mod(1.0,0.1) just doesn't make sense for it to return 0.1. It's a mystery to me.
I'm going to ask in some #math IRC channels.
#8
-
Posted 19 August 2009 - 04:21 AM
Read the posts above: you are asking the exact ratio of inexact numbers. If you perform fixpoint arithmetic, or BCD, your result will be different. Floats often are inexact, in any language.
#9
-
Posted 19 August 2009 - 11:44 AM