# Float values

Bugs will be moved here once resolved.

### » Sun Mar 31, 2013 11:19 am

http://www12.zippyshare.com/v/64346451/file.html

Steps to reproduce:
1.Set any global value eg. "anyval" to 1.2 (same hapens on 1.3 and 1.7 and many others)
2.Show str(18*anyval) in text
|
18 or some other values eg. 31, 36

Observed result:
I get 21.599999999999998 instand of 21.6 (but if i show str(18*1.2 i get 21.6)

anyval = 1.2
18*anyval = 21.599999999999998
18*1.2 = 21.6

Expected result:
anyval = 1.2
18*anyval = 21.6
18*1.2 = 21.6

Browsers affected:
Chrome: yes
Firefox: don't have Firefox
Internet Explorer: yes

Operating system & service pack:
Windows 7 64 bit SP 1

Construct 2 version:
Beta r123.2
SilentBob92PL2013-03-31 12:34:30
B
20
S
4
G
1
Posts: 10
Reputation: 1,997

### » Sun Mar 31, 2013 11:59 am

Closing. Congratulations, you have learned that computers do not operate at infinite precision; they will have rounding errors. This will happen with any programming language, framework or toolkit you ever use, because the rounding errors happen all the way down at the CPU level.

For example, calculate (1/3) * 3 to 6 decimal places of precision: you get 0.999999, not 1. That's basically what happens in computer memory, since it can't store an infinite sequence of digits!

Workaround: only ever compare if a number is within a small range, e.g. abs(a - b) < 0.001.Ashley2013-03-31 11:59:55
Scirra Founder
B
363
S
216
G
75
Posts: 23,124
Reputation: 180,557

### » Sun Mar 31, 2013 12:12 pm

But I still do not understand why if
anyval = 1.2
then in c2
18 * anyval = 21.599999999999998
though
18 * 1.2 is 21.6 not 21.599999999999999...
B
20
S
4
G
1
Posts: 10
Reputation: 1,997

### » Sun Mar 31, 2013 12:36 pm

Computers work in binary. In binary, lots of numbers are actually recurring fractions (like 0.3333...) when they can be written as a short number in decimal. I checked and 1.2 is one of these numbers that recurs infinitely in binary, and therefore gets truncated.
Scirra Founder
B
363
S
216
G
75
Posts: 23,124
Reputation: 180,557

### » Sun Mar 31, 2013 12:42 pm

OK. But if i try in C2:

18*1.2 is get me 21.6
Only if i use global(or other values) i get this 21.599999999999998

anyval = 1.2
str((18*(10*anyval))/10) this give me 21.6SilentBob92PL2013-03-31 12:46:41
B
20
S
4
G
1
Posts: 10
Reputation: 1,997

### » Sun Mar 31, 2013 1:39 pm

Whats the point? (pun intended)
These are tiny values. Its really hard to see how they could be any use in game making.
B
163
S
48
G
101
Posts: 7,462
Reputation: 72,546

### » Sun Mar 31, 2013 1:57 pm

I use layer scale and there is values like 1.2 , 0.7.
B
20
S
4
G
1
Posts: 10
Reputation: 1,997

### » Sun Mar 31, 2013 2:09 pm

Check the system expressions.
There's several ways to format the output to what you need.
B
163
S
48
G
101
Posts: 7,462
Reputation: 72,546

### » Sun Mar 31, 2013 6:21 pm

[QUOTE=SilentBob92PL]18*1.2 is get me 21.6[/quote]
Construct 2 is smart enough to calculate constant expressions at export-time with high precision. In other words, if you type 18 * 1.2 in an expression, the game doesn't actually do a multiplication. I guess the editor does this at a higher precision than the runtime which is why it gives the correct answer.

[quote]str((18*(10*anyval))/10) this give me 21.6[/QUOTE]
By multiplying by 10, you go from 1.2 (which requires an infinite number of binary digits) to 12 (which is an integer and can be represented in a finite number of binary digits). So you get a different answer. This is all exactly the same cause as I explained earlier, and will be reproducible in any programming language, framework, engine or computer system whatsoever, unless they deliberately use enhanced precision calculations (such as in financial software).
Scirra Founder
B
363
S
216
G
75
Posts: 23,124
Reputation: 180,557

### » Sun Mar 31, 2013 6:39 pm

I understand now. But I already did it, differently :)
B
20
S
4
G
1
Posts: 10
Reputation: 1,997

Next