1

I like decimal in many of my C# programs and I want to try it in other C languages.

I wrote this code in C#:

using System;

decimal get_pi();

int main() {
    Console.WriteLine("Give a number ranging from 1-100: ");
    decimal num = Console.ReadLine();
    Console.Write("Algebraic Equivalent: ");
    if (num >= get_pi()) {
        Console.WriteLine("π");
    } else {
        Console.WriteLine(num);
    }
    // Currently updating the program for giving algebraic equivalents
}

And I want to rewrite it in C++.

I realized that in both C and C++, they have no exact equivalent to the decimal type, and best you can use is double, but the fact that there's actually a supported decimal type in C (based on Is there a C equivalent of C#'s decimal type?), it got me curious if there is also an equivalent in C++.

Is there any C# decimal type equivalent in C++ or is double the best choice instead?

  • 2
    Then no, there is not a direct equivalent - depends on usage. A `long double` would be “closer”, depending on implementation. (That might be an extension..) – user2864740 Oct 28 '20 at 01:28
  • 1
    decimal in c# is a floating decimal rather than a floating binary type. Check if there is a floating decimal type in c++. That would likely be the equivalent. – jaabh Oct 28 '20 at 02:42
  • 2
    Boost multiprecision has higher-precision floating point types you can use: https://www.boost.org/doc/libs/1_72_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats.html – parktomatomi Oct 28 '20 at 02:56
  • 1
    @janzen [C++ supports everything in C's math.h](https://en.cppreference.com/w/cpp/types/climits) including FLT_RADIX so it's possible for a compliant implementation to have decimal `float` and `double` – phuclv Oct 28 '20 at 06:42

2 Answers2

5

There is not, though I believe there's a proposal out there.

But if you navigate over to IBM Fellow Mike Cowlishaw's page General Decimal Arithmetic, you'll

  • Learn more than you ever wanted to know about the topic — he pretty much wrote the IEEE 754 specification for decimal floating point — and
  • Get a link to an ANSI C implementation of it

For what it's worth, the .Net source code is open-source and on Github. If you wanted to port System.Decimal from C# to C++, you could start here:

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
1

C#'s decimal type is actually just a so called arbitrary precision float with the word arbitrary switched out with a fixed value, which can be looked up in its source (already linked by that answer). It's 28 significant digits.

Arbitrary precision floats are not included in at least C++20's standard library, but there are quite a number of libraries out there, which include that functionality. C#'s decimal is also part of the .NET library, which is strictly speaking also a library and not part of the language itself (but very tightly coupled).

Examples for libraries supporting arbitrary precision floats for C++ include:

nada
  • 2,109
  • 2
  • 16
  • 23