0

Is there any way to solve this problem?

Alex Velickiy
  • 541
  • 1
  • 7
  • 22
  • Because of how JavaScript floats are defined. This is in all browsers as it is a JavaScript standard. – devqon Oct 16 '14 at 15:11
  • 1.3 can't be translated to binary numeric system without rounding – keul Oct 16 '14 at 15:13
  • 1
    What situation do you have that makes you worried about this value? – musefan Oct 16 '14 at 15:16
  • I'm zooming in and out the interactive map on my page. And don't want to be scale factor less than 1. Zooming in just multiplies current scale factor by 1.3. And after several zooming in and several (same amount) zooming out I get scale factor 1.00000001, not 1. – Alex Velickiy Oct 16 '14 at 15:48

2 Answers2

0

Because floats (read: doubles) in javascript are not very accurate, you could round the number to use fewer decimals to hide the issue. A very hackish way to do that is to do:

var num = 1.3*1.3;
Math.round(num * 100)/100;
Snellface
  • 598
  • 5
  • 12
0

Because floating points are represented in binary, not decimal. Some decimal numbers will not be represented precisely. And unfortunately, since Javascript only has one Number class, it's not a very good tool for this job. Other languages have decent decimal libraries designed to avoid precisely this kind of error. You're going to have to either accept one-cent errors, implement a solution server-side, or work very hard to fix this.

See:

JavaScript floating point number problem

Binary representation of floating point number php/java (Server side)

Community
  • 1
  • 1
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
  • Could you please provide me with binary representation of this 1.3 in float format. Maybe not definitely as JavaScript does but maybe in some way that some programming languages do so that I could see where this trouble appears in binary representation. – Alex Velickiy Oct 16 '14 at 15:46
  • See: [how-to-get-binary-representation-of-floating-point-number](http://stackoverflow.com/questions/4729526/how-to-get-binary-representation-of-floating-point-number-in-php) – Hemerson Varela Oct 16 '14 at 17:38
  • But this thread relates to php and java codes. – Alex Velickiy Oct 16 '14 at 17:44