What is the most recommended data type to use in scientific calculation in .Net? Is it float, double or something else?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Scientific values tend to be “natural” values (length, mass, time etc) where there’s a natural degree of imprecision to start with – but where you may well want very, very large or very, very small numbers. For these values,
doubleis generally a good idea. It’s fast (with hardware support almost everywhere), scales up and down to huge/tiny values, and generally works fine if you’re not concerned with exactdecimalvalues.decimalis a good type for “artificial” numbers where there’s an exact value, almost always represented naturally as a decimal – the canonical example for this is currency. However, it’s twice as expensive asdoublein terms of storage (8 bytes per value instead of 4), has a smaller range (due to a more limited exponent range) and is significantly slower due to a lack of hardware support.I’d personally only use
floatif storage was an issue – it’s amazing how quickly the inaccuracies can build up when you only have around 7 significant decimal places.Ultimately, as the comment from “bears will eat you” suggests, it depends on what values you’re talking about – and of course what you plan to do with them. Without any further information I suspect that
doubleis a good starting point – but you should really make the decision based on the individual situation.