Home > Just Blog'ing > Math Library compatible with full .NET

Math Library compatible with full .NET

January 15th, 2009 Elze Leave a comment Go to comments

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
 

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • NuJIJ
  • Technorati
  • Yahoo! Bookmarks
Categories: Just Blog'ing Tags: ,
  1. sanny
    January 16th, 2009 at 08:50 | #1

    what is your project ?

  2. January 16th, 2009 at 20:01 | #2

    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

  3. Jeroen
    January 17th, 2009 at 10:19 | #3

    Thanks for your class! I find project Mono a good reference for missing .Net Micro functionality.

  4. January 17th, 2009 at 19:56 | #4

    @Jeroen: Thanks that is a good tip!

  5. sanny
    January 18th, 2009 at 14:30 | #5

    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

  6. January 18th, 2009 at 15:00 | #6

    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

  7. Chris
    October 11th, 2009 at 18:27 | #7

    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

  8. Elze
    October 17th, 2009 at 12:07 | #8

    Hi Chris,

    Thanks I will look into it. If so I will update the class.

    Kind regards,

    Elze

  9. Wintonlin
    January 21st, 2010 at 13:44 | #9

    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

  10. August 19th, 2010 at 18:18 | #10

    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.

  11. October 22nd, 2010 at 09:34 | #11

    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;

  12. October 25th, 2010 at 05:56 | #12

    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.

  13. Peter
    December 18th, 2010 at 02:40 | #13

    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?

  14. Valkyrie-MT
    January 5th, 2011 at 05:06 | #14

    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

  1. No trackbacks yet.