i started this year with a description how i started RE on device, only to learn that it's power cable was loose. and here we are – a quarter year later i did exactly the same. this time i've decided to memorize e (that i remember up to 2 decimal places) and pi (i remember 4 decimal places) up to 10 decimal places. what can i do with it? well – play with calc, of course. ;)
with e
it was simple – ln(e)=1
. so i started calc app on my phone: ln(2.7182818285)=1.00000000001506641592
. sweet.
problem started with pi
– sin(pi)=0
. so i started calc app on my phone:
sin(3)=0.1411…
sin(3.14)=0.0015926…
sin(3.1415)=0.054802050…
sin(3.1415926535)=0.05480366…
crap! not only value is not equal to 0
on 2nd decimal place already, but it is RAISING with improved precision! problem seemed consistent on 3 different calculator apps – looks like hardware/API thing. what can i do? i've checked if CPU type – if it is 64-bit. turned out to be Cortex-A53, so 64-bit (i.e. not an issue of 32-bit and small floating-point precision). maybe some weirdo float
vs. double
in software? let's see on my PC:
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { cout << "float: sin(3.14000000) = " << std::fixed << std::setprecision(20) << sinf(3.14f) << endl; cout << "float: sin(3.14159265) = " << std::fixed << std::setprecision(20) << sinf(3.14159265f) << endl; cout << endl; cout << "double: sin(3.14000000) = " << std::fixed << std::setprecision(20) << sin(3.14) << endl; cout << "double: sin(3.14159265) = " << std::fixed << std::setprecision(20) << sin(3.14159265) << endl; }
and we have:
float: sin(3.14000000) = 0.00159254798199981451 float: sin(3.14159265) = -0.00000008742277657348 double: sin(3.14000000) = 0.00159265291648682823 double: sin(3.14159265) = 0.00000000358979302984
hmmm… sin(3.14)
is very close to float
/double
results, but other results are more accurate. can't be that…
so i took another look at the calc app… and voila! trigonometric functions there use by default degrees – i.e. sin(180)=0
. after switching to radians, sin(3.1415926535)=0.00000000008979323846
. works. engineer – engineer never changes… ;)