Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica setting double vs. float

  1. Nov 27, 2011 #1

    I'm quite new to Mathematica, and I've been trying to use it to solve some PDE's numerically, and I eventually end up with a really large matrix I have to find the solution for. I was think the calculation would go by faster if I could take the precision of the numbers down. I think its using double and I want it to be floating pt numbers.

    I guess by precision I mean the number of digits, after the decimal point. So maybe I mean to say accuracy. In any case, is there a way that I can tell mathematica to only use lets say 5 digits after the decimal point?

    I've already read some of the documentation, about using $MaxPrecision, and WorkingPrecision, but it doesn't seem like there is a global way to set the precision. Is there? Can I just say "I want precision 5" somehow and set that for the whole notebook?

    Thanks in advanced.
  2. jcsd
  3. Nov 27, 2011 #2
  4. Dec 23, 2011 #3
    Hey Bill,

    Thanks for the link. I just had a chance to look at it, thanks to all of the end of semester crazy-ness.

    I have to admit I'm still confused. I understand that mathematica will have a limit on the type of numbers it can store, and how large they can be etc. I think I should clarify my question, I'm not that interested in how it's storing the numbers, but rather how I can tell it to store the numbers in a specific format.

    I've looked around various help guides and they mostly bring me back to tutorials similar to the one you showed me. However I still don't know how to tell mathematica that I only want it to keep the last 5 digits after the decimal point, for all of the calculations in a notebook.

    Sorry to be a pain. Thanks again for the help, and Happy Holidays.
  5. Dec 23, 2011 #4
    OK. An overly simplified but still not completely false summary.

    You may not be able to get what you want using Mathematica.

    For "most ordinary calculations" in Mathematica you can either use your fast CPU built-in hardware double precision floating point OR you can use substantially slower, often far slower, software simulated floating point. The latter is almost always when you want more than 16 digits, not less. When you tell Mathematica how many digits to use that is supposed to let it know to switch to the slow software math. And slow is exactly the opposite of what you say you want. In many cases telling it you want 16 or less digits will result in it switching to hardware floating point. And your hardware floating point has exactly the same speed for 1 digit precision as it does for 16 digit precision.

    Now that is for "most ordinary calculations." You might be able, in some cases, to get a complicated solver, like the literally thousands of pages of code that are hidden behind NDSolve[] to realize that it can stop beating its head against the wall when it gets an answer that is close enough to only 5 digits, instead of pounding on it trying to get 16 digits, and you might get some speed out of that. But you will likely spend days or weeks trying to figure out how to coax Mathematica into doing what you want instead of the default of what it wants.

    You can try giving NDSolve[theproblem,AccuracyGoal->5] or NDSolve[theproblem,PrecisionGoal->5] (and those are different) on a simplified problem and see if this gives you orders of magnitude increase in speed.

    But there isn't a switch to put Mathematica into 5 digit mode.
  6. Dec 23, 2011 #5
    Hi Bill,

    Thanks for the quick response. I understand what you mean. I guess my main issue was that this was a suggestion given to me by my project leader, so to speak, and so I went on a hunt to try and figure out if this was possible. However it doesn't seem like an easy fix, for the moment I think I will spend more time trying to get a speed up by figuring out how to solve this large sparse matrix. From what I've read about sparse matrix solutions, going that route should definitely give me a speed up.

    Thanks again for the quick reply.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook