6

In C#, there is a type called decimal (the System.Decimal structure). I have found information that shows how it is better than float and double types for some cases:

Is there any similar type for Borland C++ Builder programs?

Community
  • 1
  • 1
kokbira
  • 608
  • 1
  • 10
  • 25
  • 2
    Intel have a library http://software.intel.com/en-us/articles/intel-decimal-floating-point-math-library/ that implements the IEEE 754-2008 Decimal Floating-Point Arithmetic specification – dash Jun 20 '12 at 00:04
  • 1
    @dash, The link you provided and the Intel library seems promising. It's worth a try for someone that needs high degree of accuracy. – The Original Android Jun 20 '12 at 01:08
  • 1
    Instead of using the outdated Borland C++, can you migrate to C++ .NET and use the System.Decimal? Certainly you can create a small program in .NET given the inputs. – The Original Android Jun 20 '12 at 01:18
  • @TheOriginalAndroid, I was forced to migrate to .NET :) but I'm very familiar with BCB... I would like to know who wins on that case: .NET Vs. Borland – kokbira Jun 20 '12 at 13:12
  • 1
    @kokbira, Borland is struggling to keep up with Microsoft. Only Linux, PHP, and Apache toolset can compete with Microsoft. I suggest you migrate to .NET asap, and you're forced to anyway. At least, you won't have to deal with this decimal issue. – The Original Android Jun 20 '12 at 21:14

3 Answers3

8

The decimal type in C#, .NET's System.Decimal type, is just a floating point number stored as base-10 instead of base-2 encoding. float and double are more typical base-2 floating point numbers. That is, a double is stored as +/- x * 2^y while a decimal is stored as +/- x * 10 ^ y. That's why it does better for, as one example, financial data, which is typically expressed in terms of x * 10^-2. The IEEE standard 754 (the floating point math standard) calls this "decimal floating point" math, and defines a 32- and 64-bit version of each.

In C++, these types are implemented in the std::decimal namespace, and are called std::decimal::decimal32 and std::decimal::decimal64, in the <decimal> header. If Borland C++ builder has such a type, you will find it there. GNU's C++ library includes this header but, AFAIK it's not actually part of the standard yet, so BCB may not have it. If that's the case, you'll need to use a third-party library. @dash's example of Intel's Decimal Floating Point library is probably the best known such library, though a Google search for IEEE 754 Decimal should turn up others if, for some reason, you need them.

Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • 1
    Nice answer +1 for both that and Cthulhu – dash Jun 20 '12 at 00:50
  • 1
    @kokbira Michael's Avatar looks to be a picture of the generally all-round nice guy from Lovecraftian fiction sometimes known as Cthulhu. Except he's not very nice. – dash Jun 20 '12 at 21:05
3

These are the float types that you can use in Delphi:

single  :  4 bytes (32bits)
real    :  6 bytes (48bits)
double  :  8 bytes (64bits)
currency:  8 bytes (64bits) (this is probably what you're looking for)
extended: 10 bytes (80bits) (maps to double when you compile to x64!)

In C++ builder there seems to be a System::Currency class that mimics Delphi's built in currency type. Maybe it helps to look into that.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • Interesting. Do you know if it uses base-10 to store values? What about the range of `currency` type? – kokbira Jun 20 '12 at 13:24
2

I found this link Borland C++ Primitive Data types . View it in HTML.

There is a long double type having capacity of 10 bytes.

The document is informative. You may want to read it anyway.

The Original Android
  • 6,147
  • 3
  • 26
  • 31
  • Very informative, but I was searching a way to use float point values stored on base-10 instead of on base-2 to see if some calculations on certain programs would become more precise... – kokbira Jun 20 '12 at 13:18
  • Well, I can have more precise calculations using `long double`, for example, but I would like to evaluate a solution with base-10... – kokbira Jun 20 '12 at 13:22
  • If I understood Borland's documentation correctly, Borland float and double types use base 10 instead of base 2. – The Original Android Jun 20 '12 at 21:18