Math Library compatible with full .NET
I’m very busy building my Dare To Dream Different Challenge project. As this is a contest it’s hard to decide what to publish and what not. For my project I need a fair amount of mathematical functions like Sin, Cos, Tan, Exp, etc. These are not implemented, or in the case of Sin and Cos very inaccurate in .NET Micro Framework.
So I started the search how to calculate those functions. I made a class of it. It’s made to be a drop-in replacement for System.Math. I tried to make it as accurate as the full .NET version.
Currently supported functions:
- double Abs(double x)
- Acos(double x)
- Asin(double x)
- Atan(double x)
- Atan2(double y, double x)
- Ceiling(double x)
- Cos(double x)
- Cosh(double x)
- Exp(double x)
- Pow(double x, double y)
- Floor(double x)
- Log(double x)
- Log(double x, double newBase)
- Log10(double x)
- Max(double x, double y)
- Min(double x, double y)
- Sinh(double x)
- Sign(double x)
- Sin(double x)
- Sqrt(double x)
- Tan(double x)
- Tanh(double x)
- Truncate(double x)
You can download the Classfile here: exMath.cs
what is your project ?
Hi Snupi,
As it is an challenge I’m keeping the idea itself secret until march. But I’ll try to post as much code examples as posible
Thanks for your class! I find project Mono a good reference for missing .Net Micro functionality.
@Jeroen: Thanks that is a good tip!
hi
could you provide the location where you find the calculations (implementation)
found something on http://www.koders.com/c/fid668AA602507232FB9BF8DF177A26CC8976024E3F.aspx?s=zreturn or http://forum.unidev.com.br/phpbb2/viewtopic.php?t=43347
thx for help
Ofcourse,
The Sin, Cos, Tan functions found on:
http://www.ganssle.com/approx/approx.pdf
The Log, Log10 on:
http://en.literateprograms.org/Logarithm_Function_%28Python%29
Information about IEEE754 Floating point:
http://www.radiks.net/~rhuebner/mathlib.html
The rest is written by looking at the BSD Unix source and Wikipedia
Hi Elze,
thanks a lot for your code. But I found that the Atan2() method is buggy. To me it looks like the x and y parameters are swapped.
Could you pls. verify that?
Thanks,
Chris
Hi Chris,
Thanks I will look into it. If so I will update the class.
Kind regards,
Elze
Dear Elze:
Does your math class can be used to do the FFT or some DSP?
Or do you know where can I find the FFT code for .net MF?
Thank you very much
Lin
Hi, just wanted to write and say THANKS! for making the math library code available. I needed arctan for a project that is helping me learn the Netduino and your code worked beautifully.
Hi Elze,
I believe I found a critical error in your library, you didn’t define PI correctly!
Line 45 should be:
public static readonly double PI = 3.1415926535897931F;
Your definition for PI is incorrect; it messes with distance calculations; should be 3.1415926535897931
There also seems to be a problem in your atanx method that causes issues as well. I’ll let you know when we’ve narrowed it down and found the solution.
I have been trying to use your Log function but that does not seem to be working. It seems to get to the last while and just goes into a infinite loop.
Any ideas?
Hi Elze,
First – Thank you for this exMath.cs code!
But, I think you may have a small bug in your log function. Apparently double.Epsilon should not be used for comparisons like that. You should use 2.22045e-16 instead. See the thread here: http://forums.netduino.com/index.php?/topic/972-whats-going-on-with-the-operator/page__gopid__7052