4.1 - 0.1 = 3.9999999999999996 (Apparently)

rp-7

#1

Hi,

I’m trying to perform a very simple maths function in axure pro v7.

I would like to subtract 0.1 from 4.1.

As you can see on the attached RP file I have a button with an onclick event as follows

set value of OnLoadVariable equal to “[[OnLoadVariable - 0.1]]”

I have set the OnLoadVariable to 4.1.

Upon clickin the button the result is returned as 3.9999999999999996

Can anyone shed any light on this?

Thanks

Tim
Maths Function 1.rp (55.9 KB)


#2

Seems like a bug, I’d report it!


#3

This is a floating point math issue and is a common issue in computing when using floating point representations of decimals and is not unique to Axure.

float - What causes floating point rounding errors? - Programmers Stack Exchange

For example, try typing 4.1 - 0.1 in the console in your browser, you’ll see the same thing. Or try 0.1 * 0.2. Same problem.

I’m not sure what solutions there are. In other types of programming, you would usually avoid or mitigate the issue by using different representations of numbers.

One possibility is to avoid using decimals in your calculations if you’re running into this:

( 41 - 1 ) / 10 == 4.1 - 0.1

Also using .toFixed(0) solves it.

( 4.1 - 0.1 ).toFixed(0)

Local variables not summing up numerical values properly
Trouble with decimals
#4

Hi,

Thanks for your replies.

nkrisc, having read that post I think you’re right.

If I subtract 1.25 from 4.125 the calculation is correct.

I will try to avoid using decimals, although this may be difficult.

Cheers

Tim


#5

Try using .toFixed(0) if you like. I did not test it extensively, but it correct the specific calculation that was causing a problem for you.

( 4.1 - 0.1).toFixed(0) == 4

Or if you only every want to go to the first decimal place, you could multiply all your numbers by 10 first, then add or subtract, then divide by 10 after. Certainly not elegant or fun, but it might work for you.


#6

Strangely, ( 4.1 - 0.1).toFixed(0) still yeilded the same result. Is that the correct syntax?


#7

It works for me using the following:

[[(OnLoadVariable - 0.1).toFixed(0)]]

and also for:

[[(4.1 - 0.1).toFixed(0)]]

#8

James’ examples above worked as expected for me as well. I’m using V7 release 3183.


#9

I removed the event then re-added it, it is now working.

Thank you guys for your help.

Tim


closed #10

unlisted #11