SUM - why am I getting a different amount when I use the sum and usage calculator)?

Good day.

I noticed an oddity when calculating the amount in the field.

Vaues table:

Vaues table

Enter the field - float.

I make the selected amount:

SELECT SUM(cost) as cost FROM Table

      

As a result, I get sum

= 20.47497010231018;

sum result

I am using calculator) and I get sum

= 20.47497

Please tell me why I am getting different results?

+3


source to share


3 answers


Read it here: http://floating-point-gui.de/

So the problem is that floating point numbers are an internal implementation for decimal numbers in hardware and used by most languages ​​and applications, do not map 1 to 1 numbers in base 10 as we are using. Base values ​​are expressed in base 2 and have limited precision. Some numbers that can be represented by multiple digits in decimal notation may require many more digits in their native format, leading to rounding errors.



The article above explains this in detail.

+1


source


These differences are not enough to be of concern in most situations.

Floating point numbers are known to lag slightly behind their intended values ​​due to the number of bits and how floating point numbers are stored in binary. For example, as shown here , the decimal value 0.1

has the actual value double

0.10000000149011612

. Close, but not exactly.



The technique I have seen in some applications requiring perfectly accurate latitude and longitude numbers is that they will store values ​​in integral data types that are equivalent to floats multiplied by some cardinality 10. For example, GeoPoint in Google Maps API v1 by Android is measured in micro degrees. Instead of latitude and longitude, such as 54.123428, 60.809234

to save the values accurately, they will ints: 54123428, 60809234

. To depict this, they will name the variables latitudeE6

and longitudeE6

to indicate that this is actual latitude or longitude multiplied by 1E6 (scientific notation 10 ^ 6).

+1


source


Use DECIMAL(16, 4)

(for example) the type for currency columns, where 4

is the number of digits after.

You will have to trim the trailing zeros though, for example, 10

will appear 10.0000

. However, this is easy to do.

+1


source







All Articles