Mathematica setting double vs. float

In summary, you can either use your hardware double precision floating point or software simulated floating point when solving problems with Mathematica. If you want to solve a problem with fewer digits after the decimal point, you will likely need to use software simulated floating point.
  • #1
tau1777
30
0
Hi,

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 let's 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.
 
Physics news on Phys.org
  • #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.
 
  • #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.
 
  • #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.
 

1. What is the difference between using double and float in Mathematica?

In Mathematica, double and float refer to different types of numerical data. Double is a data type that stores numbers with higher precision, while float stores numbers with lower precision. This means that double can hold more significant digits and is more accurate than float.

2. How do I specify which type of numerical data to use in Mathematica?

To specify whether you want to use double or float for numerical data in Mathematica, you can use the functions SetPrecision or SetAccuracy. SetPrecision allows you to specify the number of significant digits, while SetAccuracy allows you to specify the number of accurate digits.

3. Which type of numerical data should I use for my calculations in Mathematica?

The type of numerical data you should use depends on the level of accuracy you need for your calculations. If you need a high level of precision, then you should use double. However, if your calculations do not require a high level of accuracy, then using float can save memory and improve performance.

4. Can I change the type of numerical data during my calculations in Mathematica?

Yes, you can change the type of numerical data during your calculations in Mathematica. This can be done by using the functions N or SetPrecision. N converts the data to machine precision, while SetPrecision allows you to specify the precision of the data.

5. Is there a default type of numerical data used in Mathematica?

Yes, the default type of numerical data used in Mathematica is double. This is because it offers higher precision and accuracy compared to float. However, you can change this default setting by using the function $MachinePrecision, which specifies the number of decimal digits used for machine-precision numbers.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
256
Replies
4
Views
920
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Computing and Technology
Replies
4
Views
758
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
931
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
8K
Back
Top