473,320 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

float vs double

guy
Can someone point me to page that gives a decision tree on deciding when to
use float or double in a C# application including performance impacts of
using one over the other. And when is decimal used?

Thanks
Nov 16 '05 #1
5 66165
guy <wi*********@hotmail.com> wrote:
Can someone point me to page that gives a decision tree on deciding when to
use float or double in a C# application including performance impacts of
using one over the other.
I believe there isn't much performance impact at all (in terms of
speed) in using double instead of float. However, there is the memory
impact - if you're using an awful lot of floating point numbers and you
have enough accuracy in float (6-7 significant figures) then use float,
but by default I'd use decimal.
And when is decimal used?


When the sample data is accurate in decimal form (eg with currency) and
needs to stay that way. There's a significant performance difference
between float/double and decimal.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
guy
Jon,

I award you twice:
1. fastest response time
2. most comprehensive answers

Many thanks and this brings me to another question:

Yes - this application is 100% financial (futures market data) so rarely
needs accuracy beyond about 4 decimal places.

You said: "There's a significant performance difference between float/double
and decimal."

You didn't say if the performance was improved or degraded by using decimal
instead of float/double.

Thanks
Guy
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
guy <wi*********@hotmail.com> wrote:
Can someone point me to page that gives a decision tree on deciding when
to
use float or double in a C# application including performance impacts of
using one over the other.


I believe there isn't much performance impact at all (in terms of
speed) in using double instead of float. However, there is the memory
impact - if you're using an awful lot of floating point numbers and you
have enough accuracy in float (6-7 significant figures) then use float,
but by default I'd use decimal.
And when is decimal used?


When the sample data is accurate in decimal form (eg with currency) and
needs to stay that way. There's a significant performance difference
between float/double and decimal.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
guy <wi*********@hotmail.com> wrote:
I award you twice:
1. fastest response time
2. most comprehensive answers
LOL :)
Many thanks and this brings me to another question:

Yes - this application is 100% financial (futures market data) so rarely
needs accuracy beyond about 4 decimal places.
Right. Bear in mind, however, that as soon as you've performed a few
operations on the data, the accuracy will have decreased. For financial
applications, I'd suggest either using Decimal or using scaled integers
with the appropriate business rounding rules etc.
You said: "There's a significant performance difference between float/double
and decimal."

You didn't say if the performance was improved or degraded by using decimal
instead of float/double.


Sorry - degraded. Float/double is performed in hardware, but decimal
needs to be done in software, effectively.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
guy
Many thanks Jon,

I've also found this link which shows the performance between
float/double/decimal:
http://www.geocities.com/herong_yang...rformance.html

Thanks again for your advice.

Guy
Nov 16 '05 #5
guy <wi*********@hotmail.com> wrote:
I've also found this link which shows the performance between
float/double/decimal:
http://www.geocities.com/herong_yang...rformance.html


Hmm... the fact that he's doing stuff with variables passed by
reference is a bad first sign. That changes the performance
characteristics, and isn't typical of real use.

The accuracy "problem" with decimal is (I believe - I haven't done a
detailed analysis of the program) that it doesn't cope with very small
numbers as well as double. (Have a look into how decimals and doubles
are stored for more information about this.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
9
by: Sisyphus | last post by:
Hi, I have some software that does the following (in an attempt to determine whether the double x, can be represented just as accurately by a float): void test_it(double x) { float y = x;...
13
by: maadhuu | last post by:
hello , i would like to know as to why double is more efficient than float . thanking you, ranjan.
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
5
by: Kubik | last post by:
Hi! Let's see, we got: float var=4.6f; //as we know 414/4.6 shoud be equal to 90 but Math.Ceiling(414/var) gives us 91 but (414/var).ToString() prints '90'.
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.