Details
-
Type:
Bug
-
Status: Closed
-
Priority:
Critical
-
Resolution: Not a Bug
-
Affects Version/s: 10.0.4
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Environment:Centos 6.3
Description
Something is not working correctly on handling decimals. I am still digging to the source, but this example returns NULL and should return 0.
SELECT
(ACOS(SIN(RADIANS(26.197506)) * SIN(RADIANS(26.197506)) + COS(RADIANS(26.197506)) * COS(RADIANS(26.197506)) * COS(RADIANS(-80.107953) - RADIANS(-80.107953)))*3959) AS `Distance`
However, changing the numbers to one that would not round up if rounded as below returns the proper 0
SELECT
(ACOS(SIN(RADIANS(26.197504)) * SIN(RADIANS(26.197504)) + COS(RADIANS(26.197504)) * COS(RADIANS(26.197504)) * COS(RADIANS(-80.107953) - RADIANS(-80.107953)))*3959) AS `Distance`
Gliffy Diagrams
Attachments
Activity
- All
- Comments
- Work Log
- History
- Activity
- Transitions
It's not related to decimals, but to floats. (DECIMAL is the exact numeric type, FLOAT is approximate).
ACOS(), SIN(), COS() — they use floating-point and implicitly convert their arguments to FLOAT.
You see NULL because
MariaDB [test]> SELECT SIN(RADIANS(26.197506)) * SIN(RADIANS(26.197506)) + COS(RADIANS(26.197506)) * COS(RADIANS(26.197506)) as `sin^2+cos^2`;
--------------------
--------------------
--------------------
1 row in set (0.00 sec)
Which is mathematically incorrect, but calculations with floating point numbers are approximate and that result above is not very surprising.
As a workaround, you can wrap your sin^2+cos^2 part of the formula in LEAST(1, ...).