std.math module

sarn via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 6 17:21:34 PDT 2017


On Sunday, 6 August 2017 at 23:33:26 UTC, greatsam4sure wrote:
> import std.math;
> import std.stdio;
>
> cos(90*PI/180) = -2.7e-20 instead of zero. I will appreciate 
> any help. thanks in advance.
>
> tan(90*PI/180) = -3.689e+19 instead of infinity. What is the 
> best way to use this module

That's just floating point maths for you.  You're not putting 
exactly pi/2 into cos, just a good floating point approximation.  
What you're getting out isn't exactly 0, either, just a good 
floating point approximation.  (-2.7e-20 is really, really small.)

Here's a good talk from DConf 2016: 
https://www.youtube.com/watch?v=97bxjeP3LzY

If you need exact maths, you'll need a symbolic manipulation 
library (never used one in D, but there was a discussion recently 
https://forum.dlang.org/thread/ghihookwgzxculshibyc@forum.dlang.org).  You don't need this for most practical applications, though.


More information about the Digitalmars-d-learn mailing list