Euphoria
Ticket #903:
sin() and cos() returns incorrect results when translated
-
Reported by
cargoan
Sep 29, 2014
--file sin.ex
integer i = 1
atom a = 1.0
printf(1, "\n\nsin(%d) = %.10f\n", {i, sin(i)})
printf(1, "sin(%f) = %.10f\n", {a, sin(a)})
printf(1, "\ncos(%d) = %.10f\n", {i, cos(i)})
printf(1, "cos(%f) = %.10f\n\n", {a, cos(a)})
In terminal:
eui sine.ex && euc sine.ex && ./sine && ./sine
sin(1) = 0.8414709848
sin(1.000000) = 0.8414709848
cos(1) = 0.5403023059
cos(1.000000) = 0.5403023059
Build directory: build-126339/
Translating code, pass: 1 2 3 generating
Compiling with GCC
Compiling 12% init-.c
Compiling 50% sine.c
Compiling 75% main-.c
Linking 100% ../sine
sin(1) = 1294776.0000000000
sin(1.000000) = 0.8414709848
cos(1) = 1294780.0000000000
cos(1.000000) = 0.5403023059
sin(1) = 1029048.0000000000
sin(1.000000) = 0.8414709848
cos(1) = 1029052.0000000000
cos(1.000000) = 0.5403023059
if parameter is integer the result is wrong and it varies each call or execution. Sorry my english.
My system is Archlinux 64bits
Details
1. Comment by SDPringle
Sep 29, 2014
I cannot reproduce this on 32-bit Linux. Looking at the source code generated we have e_sin and unary_op(SIN, ..). I suggest: Put a break point in e_sin and see if a is correct, then look at the value returned and see if that is correct.
2. Comment by mattlewis
Sep 30, 2014
I get different (but still bad) results on 64-bit Linux when translating a 64-bit binary. Results are correct for 32-bits.
3. Comment by mattlewis
Sep 30, 2014
We need to add some stuff to euphoria.h for the translator. Without definitions, the compiler assumes they return 32-bit values which is wrong on a 64-bit architecture.
4. Comment by cargoan
Sep 30, 2014
tan() is bad, sqrt(), arctan() and log() are ok.
5. Comment by cargoan
Sep 30, 2014
solved adding to euphoria.h: ... ... object e_log(object a); object De_log(d_ptr a); /* added */ object e_sin(object a); object De_sin(d_ptr a); object e_cos(object a); object De_cos(d_ptr a); object e_tan(object a); object De_tan(d_ptr a); ....
6. Comment by mattlewis
Oct 01, 2014
Added several imports:
See: hg:euphoria/rev/e9a250c9e114
7. Comment by DerekParnell
Oct 01, 2014
Would it be worthwhile to check all the built-in function signatures to ensure that they are 64-bit ready, and not just the transcendentals.
8. Comment by mattlewis
Oct 01, 2014
Probably worth-while. I just went through all of the stuff in be_runtime.c that are in the optable for binary ops to see what was used by the translator and missing from euphoria.h.
9. Comment by DerekParnell
Oct 01, 2014
Further test cases should probably include things like ...
test_equal("sin(atom) = sin(int)", sin(1.000000), sin(1))
10. Comment by SDPringle
Jan 31, 2015
See: hg:euphoria/rev/bb17603a0b00
changeset: 6298:bb17603a0b00 branch: 4.0 user: Shawn Pringle <shawn.pringle@gmail.com> date: Mon Jan 26 20:47:45 2015 -0300 files: tests/t_math.e description:
- added test cases for ticket 903