Click here to Skip to main content
15,887,135 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
#realJSOP22-Mar-24 10:16
mve#realJSOP22-Mar-24 10:16 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon23-Mar-24 5:11
professionalJeremy Falcon23-Mar-24 5:11 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
#realJSOP24-Mar-24 11:54
mve#realJSOP24-Mar-24 11:54 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon26-Mar-24 10:42
professionalJeremy Falcon26-Mar-24 10:42 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
rjmoses23-Mar-24 0:00
professionalrjmoses23-Mar-24 0:00 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon23-Mar-24 5:15
professionalJeremy Falcon23-Mar-24 5:15 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
charlieg31-Mar-24 4:31
charlieg31-Mar-24 4:31 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
Stacy Dudovitz23-Mar-24 13:41
professionalStacy Dudovitz23-Mar-24 13:41 
After decades of writing software for industrial, medical, financial and LoB (Line of Business) applications, I found that the following guidelines work:

1) Financial and money, I always use the decimal type for currency, and the smallest sized type that affords me the precision I need. So why would I use any other type in a currency/money app?

Simple example: I'm writing a trading application, where the strike price will be stored in a decimal type, and the number of shares will be store in a float type. Why not use a decimal type for the number of shares? Because there's no guarantee that it will be 3 places to the right of the decimal (that's typical, but not a hard fast rule). I chose float because its the smallest type that offers the precision I seek. By smallest I mean that a double is typically twice the size of a float.

For those that are tempted to respond that floats are 64 bits, and doubles are 128 bit, not necessarily. That's a very PC centric view.

Note: These guidelines typically, but not always, apply to LoB

2) For medical and industrial, which usually require floating point precision to store values that may not be the same as the formatting to the display, I use floats and doubles, using the smallest type that affords the precision required by the application under development.

What do I mean by the smallest type and precision?

The size of the type refers to how large does the floating point type have to be in order to maintain the level of precision (the number of places after the decimal point) while not losing appreciable loss to rounding and implicit conversions (more on that below).

Caveats:

There are several other considerations when choosing and writing floating point code.

A) Rounding loss: This refers to how precise a resulting value is after some operation is performed on it. This is not limited to mathematical operations only (multiplication, division), this also applies to any library calls used to generate a new value e.g. sqrt(...).

B) Conversions: Be very very careful about mixing types i.e. decimal, float and double. When a smaller type is promoted to a larger type, it may introduce random "precision" that actually makes the new value deviate farther from the mean i.e. the new value strays farther from representing the true value.

So for example:
C#
float pi = 3.1415927;
float radius = 5.2;
double circumference = 2.0f * radius * pi;

circumfrence may be holding a value that looks to be precise, but in fact the implicit conversion from float to double introduced some randomness to the number in the right side of the decimal point.

C) Truncations: Conversely storing the result of a larger type in a smaller type will remove the precision to the right of the decimal point.

So for example:

Formula: Simple Interest (SI) = Principal (P) x Rate (R) x Time (T) / 100.
C#
float si;
double principal;
double rate;
int time;
si = principal * rate * time / 100;

We have to watch two types that are involved in this calculation - time, which is an int type, and si (simple interest) which is a float type.

Why would we use float instead of double for the simple interest si?

Suppose you have a method that takes a float as one of its arguments:
float DoSomeCalcOnSimpleInterest(float si)
{
   ...
}

The only type that this call will accept is a float, so the result must be a float.

Why not make all of the arguments type float? You might need the extra precision during the calculation, even if the answer can be sufficiently represented in a float.

D) Illegal operations: Examples typically include division by 0, generating NaNs (that's a whole other ball of wax), etc. Typically the way to guard against these edge cases, in addition to parameter validation, is to frame the code in an exception handler. This might not always be possible, such as in embedded development where the language support for exception handling may not be included as memory space is a precious commodity.

You might also be writing code on a target processor that does not trap arithmetic exceptions i.e. there is no support to trap/propagate exceptions to the upper layer where the application lives. These insidious types of errors/bugs are difficult to trace if care is not taken in the development of the codebase.

This missive is not meant to be an exhaustive treatise into implementing floating point support in an application. It should, however, provide you with starting guidelines as you design and development your software.

If you are developing in C++ or C#, you might want to take a look at this:

Implicit Operators in C#: How To Simplify Type Conversions
(https://www.codeproject.com/Articles/5378558/Implicit-Operators-in-Csharp-How-To-Simplify-Type?msg=5991279#xx5991279xx)
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon24-Mar-24 4:47
professionalJeremy Falcon24-Mar-24 4:47 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Stacy Dudovitz24-Mar-24 15:59
professionalStacy Dudovitz24-Mar-24 15:59 
GeneralRe: What's y'all's favorite way of dealing with floating point precision? Pin
Jeremy Falcon26-Mar-24 10:41
professionalJeremy Falcon26-Mar-24 10:41 
AnswerRe: What's y'all's favorite way of dealing with floating point precision? Pin
mdblack9825-Mar-24 3:13
mdblack9825-Mar-24 3:13 
GeneralWordle 1,006 Pin
StarNamer@work20-Mar-24 14:04
professionalStarNamer@work20-Mar-24 14:04 
GeneralRe: Wordle 1,006 Pin
Amarnath S20-Mar-24 14:08
professionalAmarnath S20-Mar-24 14:08 
GeneralRe: Wordle 1,006 Pin
Sandeep Mewara20-Mar-24 15:42
mveSandeep Mewara20-Mar-24 15:42 
GeneralRe: Wordle 1,006 Pin
Shane010320-Mar-24 17:51
Shane010320-Mar-24 17:51 
GeneralRe: Wordle 1,006 (3/6) Pin
Jeremy Falcon20-Mar-24 19:19
professionalJeremy Falcon20-Mar-24 19:19 
GeneralRe: Wordle 1,006 Pin
GKP199220-Mar-24 19:22
professionalGKP199220-Mar-24 19:22 
GeneralRe: Wordle 1,006 Pin
OriginalGriff20-Mar-24 20:06
mveOriginalGriff20-Mar-24 20:06 
GeneralRe: Wordle 1,006 - 4 4 me Pin
pkfox20-Mar-24 22:15
professionalpkfox20-Mar-24 22:15 
GeneralRe: Wordle 1,006 Pin
Sander Rossel20-Mar-24 22:25
professionalSander Rossel20-Mar-24 22:25 
GeneralRe: Wordle 1,006 Pin
ChandraRam20-Mar-24 23:23
ChandraRam20-Mar-24 23:23 
GeneralRe: Wordle 1,006 Pin
Cp-Coder21-Mar-24 0:02
Cp-Coder21-Mar-24 0:02 
GeneralWhat did one cannibal say to his buddy after eating a clown? Pin
jmaida20-Mar-24 11:37
jmaida20-Mar-24 11:37 
GeneralIf you ever want to see pure, unfiltered joy Pin
honey the codewitch20-Mar-24 8:18
mvahoney the codewitch20-Mar-24 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.