float, double and decimal

I

illegal.prime

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
 
M

Marc Gravell

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
 
I

illegal.prime

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
 
J

Jon Skeet [C# MVP]

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.
 
I

illegal.prime

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top