473,396 Members | 2,011 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,396 software developers and data experts.

float, double and decimal

Hi all, I have a class that I would like to use to hide the data type
I'm using to store decimal point numbers. Failing all else, I will
just use object and do conversion/casts as necessary. But I was
wondering if I could use double to house all basic/primitive decimal
numbers (i.e. only the float, double and decimal types) in C#. This
type would also have to allow numerical greater than and less than
comparisons.

So I'm pretty sure that it would be safe to use double to house any
float - but what about decimal. Could double be used to house any
decimal?

Thanks,
Novice

Jun 28 '06 #1
4 5048
Well, no.

Double is an approximated format; it has severe difficulty representing
most numbers /accurately/ (even things like 3.1 [random guess] can get
turned into 3.0998873 [again, random guess]), and cannot be trusted
/especially/ for == operations. Decimal is exact, but more restrictive
in other ways.

Personally I would advise storing each number in it's native format,
and then only casting if necessary; prehaps something grungy is
possible involving generics and late-casting, but I'm not sure I would
recommend it... There are some arbitrary (read: big) precision/scale
data-types available that might suit this purpose (some in the J#
libraries) - but it sounds like a JCB to to crack a wallnut.

Marc

Jun 28 '06 #2
Thanks - you saved me some work - I didn't realize that in some cases
floats were more accurate than doubles.

Too bad .Net doesn't have a non-basic/primitive type that handles
floats and doubles - like a wrapper around floats and doubles. I seem
to recall that Java has a wrapper class for each of its basic/primitive
types - too bad there isn't an aggregate wrapper - or is there?

Novice

Marc Gravell wrote:
Well, no.

Double is an approximated format; it has severe difficulty representing
most numbers /accurately/ (even things like 3.1 [random guess] can get
turned into 3.0998873 [again, random guess]), and cannot be trusted
/especially/ for == operations. Decimal is exact, but more restrictive
in other ways.

Personally I would advise storing each number in it's native format,
and then only casting if necessary; prehaps something grungy is
possible involving generics and late-casting, but I'm not sure I would
recommend it... There are some arbitrary (read: big) precision/scale
data-types available that might suit this purpose (some in the J#
libraries) - but it sounds like a JCB to to crack a wallnut.

Marc


Jun 28 '06 #3
<il***********@gmail.com> wrote:
Thanks - you saved me some work - I didn't realize that in some cases
floats were more accurate than doubles.
Floats are never more accurate than doubles. I'm not sure where you got
that impression from in Marc's post. Decimal is very different to
float. See
http://www.pobox.com/~skeet/csharp/floatingpoint.html
http://www.pobox.com/~skeet/csharp/decimal.html
Too bad .Net doesn't have a non-basic/primitive type that handles
floats and doubles - like a wrapper around floats and doubles. I seem
to recall that Java has a wrapper class for each of its basic/primitive
types - too bad there isn't an aggregate wrapper - or is there?


Well, you could have a Number type which would always consist of a
float, double or decimal, and could have operations which worked with
other Numbers which used the same type, but it would be pretty clunky -
I don't really see the benefit.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 28 '06 #4
Right or wrong - I ended up using my own wrapper class with operator
overloads. Its real benefit was recognized in all the different
integer types (int, uint, long, and ulong).

Novice

Jon wrote:
<il***********@gmail.com> wrote:
Thanks - you saved me some work - I didn't realize that in some cases
floats were more accurate than doubles.


Floats are never more accurate than doubles. I'm not sure where you got
that impression from in Marc's post. Decimal is very different to
float. See
http://www.pobox.com/~skeet/csharp/floatingpoint.html
http://www.pobox.com/~skeet/csharp/decimal.html
Too bad .Net doesn't have a non-basic/primitive type that handles
floats and doubles - like a wrapper around floats and doubles. I seem
to recall that Java has a wrapper class for each of its basic/primitive
types - too bad there isn't an aggregate wrapper - or is there?


Well, you could have a Number type which would always consist of a
float, double or decimal, and could have operations which worked with
other Numbers which used the same type, but it would be pretty clunky -
I don't really see the benefit.

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


Jun 29 '06 #5

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

Similar topics

3
by: Erik2000 | last post by:
If someone defines a float and double in C++, does that say anything about how many digits the number will hold at maximum and how many places there will be after the decimal point? Or is this...
10
by: Mad Butch | last post by:
void Test() { float fValue = 0.5678f; // Value is 0.567800 double dValue = (double)fValue; // Value is 0.56779998540878 } Is there anyway I can round a float at 6 positions behind the...
8
by: Quinn Kirsch | last post by:
Hi all, the following c# example baffles me. my understanding of the managed code of visual .net is that all casts like this would be safe, but this example seems to contradict this notion. if...
5
by: guy | last post by:
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...
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...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
17
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.