Same double precision operations, different results

From: Daniel Vázquez <daniel2d2art(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Same double precision operations, different results
Date: 2014-02-13 17:16:07
Message-ID: CA+KJVfw-1tCwiT42KafeC2v-wtNEB1nZDLCajh-tpDoZ=sAvTg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Please can someone tell me why this behavior? Thx!!

I know it's calculating over coordinates on same points, but the operations
of values (all double precision) differs even forcing with cast

(*) Here some double precision calculations and obtaining values

# Select
# cos(radians(39.9826557)) as f1,
# cos(radians(39.9826557)) as f2,
# cos(radians(-0.04773120000004383) - radians(-0.04773120000004383)) as
f3,
# sin(radians(39.9826557)) as f4,
# sin(radians(39.9826557)) as f5;
f1 | f2 | f3 | f4 | f5

-------------------+-------------------+----+-------------------+-------------------
0.766238989559398 | 0.766238989559398 | 1 | 0.642555686986733 |
0.642555686986733
(1 row)

________________________________________

(*) Here above same obtaining values involved in simple operations

# select (0.766238989559398 * 0.766238989559398 * 1 + 0.642555686986733 *
0.642555686986733) calc;
calc
----------------------------------
0.999999999999999633651488135693
(1 row)

________________________________________

(*) Here above queries playing together ---> Result = 1 ????? NOOOO! It
should be 0.999999999999999633651488135693

# select (v.f1 * v.f2 * v.f3 + v.f4 * v.f5) calc from ( select
cos(radians(39.9826557)) as f1,
cos(radians(39.9826557)) as f2,
cos(radians(-0.04773120000004383) - radians(-0.04773120000004383)) as f3,
sin(radians(39.9826557)) as f4,
sin(radians(39.9826557)) as f5 ) as v;
calc
------
1
(1 row)

________________________________________

(*) Yep, this produces problems with acos. Here acos working directly with
the above values. OK this is like acos(0.999999999999999633651488135693)

# select acos(0.766238989559398 * 0.766238989559398 * 1 + 0.642555686986733
* 0.642555686986733);
acos
----------------------
2.58095682795179e-08
(1 row)

________________________________________

(*) Yep, you can see same result

# select acos(0.999999999999999633651488135693);
acos
----------------------
2.58095682795179e-08
(1 r

________________________________________

(*) ok acos values from -1 to 1 you can see acos(1) here

# select acos(1);
acos
------
0
(1 row)

________________________________________

(*) Why this calculation produces 1 and not 0.999999999999999633651488135693
?????????

# select (
cos(radians(39.9826557))
* cos(radians(39.9826557))
* cos(radians(-0.04773120000004383) - radians(-0.04773120000004383))
+ sin(radians(39.9826557))
* sin(radians(39.9826557))
) calc;
calc
------
1
(1 row)

________________________________________

(*) any way, the result is 1, then if acos(1) get results, this must get
same result .... NOPS!! "input is out of range"

# select acos(
# cos(radians(39.9826557))
# * cos(radians(39.9826557))
# * cos(radians(-0.04773120000004383) - radians(-0.04773120000004383))
# + sin(radians(39.9826557))
# * sin(radians(39.9826557))
# );
ERROR: input is out of range

________________________________________

(*) You are thinking: it's some cast problem ... NOPS!! "input is out of
range"

# select acos( cast (
cos(radians(39.9826557))
* cos(radians(39.9826557))
* cos(radians(-0.04773120000004383) - radians(-0.04773120000004383))
+ sin(radians(39.9826557))
* sin(radians(39.9826557)) as double precision )
);
ERROR: input is out of range

Really, I can get the reason!!! Some idea? Workaround? Thx

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2014-02-13 17:26:48 Re: Same double precision operations, different results
Previous Message Andres Freund 2014-02-13 17:13:04 Re: truncating pg_multixact/members