Thread: can I use "decimal" type also in C++?

  1. #1
    Registered User I C everything's Avatar
    Join Date
    Apr 2019
    Posts
    101

    Question can I use "decimal" type also in C++?

    Hi
    can I use "decimal"(C#128bit type) type also in C++,maybe with some custom library for it?
    you tell me you can C,why dont you C your own bugs?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    long double tends to be 96 bit type; I suggest you try it and see how long it is.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    From: Types - C# language specification | Microsoft Docs

    The decimal type

    The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits.

    The finite set of values of type decimal are of the form (-1)^s * c * 10^-e, where the sign s is 0 or 1, the coefficient c is given by 0 <= *c* < 2^96, and the scale e is such that 0 <= e <= 28.The decimal type does not support signed zeros, infinities, or NaN's. A decimal is represented as a 96-bit integer scaled by a power of ten. For decimals with an absolute value less than 1.0m, the value is exact to the 28th decimal place, but no further. For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation. In the float and double representations, such numbers are often infinite fractions, making those representations more prone to round-off errors.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > can I use "decimal"(C#128bit type) type also in C++,maybe with some custom library for it?
    If only someone had invented a way to search the web for things....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by stahta01 View Post
    long double tends to be 96 bit type; I suggest you try it and see how long it is.
    80 bits, with 64 bits precision. 'long double' tends to occupy 16 bytes on memory due to alignment.

    There is an extension on GCC (and, I believe, clang): __float128 (128 bits, 113 bits precision).

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    66
    May be this source could be useful:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <float.h>
    int main(int argc, char** argv) {
    
       //printf("CHAR_BIT    :   %d\n", CHAR_BIT);
      
        printf("\nLength of Data typs\nand their limits of Valuews\n\n");    
        printf("char.......: %ld Byte  in Bit: %d\n", sizeof(char), CHAR_BIT);
        printf("signed char...:   %d  to %d\n", CHAR_MAX, CHAR_MIN);
        printf("unsigned char.:   0    to  %d\n\n", UCHAR_MAX);
        //printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
        //printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
        printf("short.......:   %ld Byte\n", sizeof(short));    
        printf("SHRT_MAX    :   %d\n", SHRT_MAX);
        printf("SHRT_MIN    :   %d\n", SHRT_MIN);
        printf("USHRT_MAX   :   %d\n\n", (unsigned short) USHRT_MAX);
        printf("Datentyp int    %ld Byte\n", sizeof(int));
        printf("INT_MAX     :   %d\n", INT_MAX);
        printf("INT_MIN     :   %d\n", INT_MIN);
        printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
        printf("\nlong        :   %ld Byte\n", sizeof(long));
        printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
        printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
        printf("ULONG_MAX   :   %lu\n\n", (unsigned long) ULONG_MAX);
        printf("float       :   %ld Byte\n", sizeof(float));
        printf("FLT_MAX     :   ±%f\n",  FLT_MAX);
        printf("FLT_MAX     :   ±%E\n",  FLT_MAX);
        printf("FLT_MIN     :   %f\n",  FLT_MIN);
        printf("FLT_DIG     :   %d\n",  FLT_DIG);
        printf("\ndouble      :   %ld Byte\n", sizeof(double));
        //printf("DBL_MAX     :   %lf\n",  DBL_MAX);
        printf("DBL_MAX     :   ±%lG\n",  DBL_MAX);
        printf("DBL_MIN     :   %lf\n",  DBL_MIN);
        printf("DBL_DIG     :   %d\n",  FLT_DIG);
        printf("\nDatentyp long double: %ld Byte\n\n", sizeof(long double));
        printf("LDBL_MIN    :   %LG\n",  LDBL_MIN);
        printf("_DBL_MAX    :   %LG\n",  LDBL_MAX);    
        printf("LDBL_DIG    :   %d\n",  LDBL_DIG);
        
        return 0;
    }
    This lines are not shown by my post, no matter why:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <float.h>
    Last edited by rusyoldguy; 04-20-2020 at 02:09 AM.

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    2
    Quote Originally Posted by rusyoldguy View Post
    May be this source could be useful:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <float.h>
    int main(int argc, char** argv) {
    
       //printf("CHAR_BIT    :   %d\n", CHAR_BIT);
      
        printf("\nLength of Data typs\nand their limits of Valuews\n\n");    
        printf("char.......: %ld Byte  in Bit: %d\n", sizeof(char), CHAR_BIT);
        printf("signed char...:   %d  to %d\n", CHAR_MAX, CHAR_MIN);
        printf("unsigned char.:   0    to  %d\n\n", UCHAR_MAX);
        //printf("SCHAR_MAX   :   %d\n", SCHAR_MAX);
        //printf("SCHAR_MIN   :   %d\n", SCHAR_MIN);
        printf("short.......:   %ld Byte\n", sizeof(short));    
        printf("SHRT_MAX    :   %d\n", SHRT_MAX);
        printf("SHRT_MIN    :   %d\n", SHRT_MIN);
        printf("USHRT_MAX   :   %d\n\n", (unsigned short) USHRT_MAX);
        printf("Datentyp int    %ld Byte\n", sizeof(int));
        printf("INT_MAX     :   %d\n", INT_MAX);
        printf("INT_MIN     :   %d\n", INT_MIN);
        printf("UINT_MAX    :   %u\n", (unsigned int) UINT_MAX);
        printf("\nlong        :   %ld Byte\n", sizeof(long));
        printf("LONG_MAX    :   %ld\n", (long) LONG_MAX);
        printf("LONG_MIN    :   %ld\n", (long) LONG_MIN);
        printf("ULONG_MAX   :   %lu\n\n", (unsigned long) ULONG_MAX);
        printf("float       :   %ld Byte\n", sizeof(float));
        printf("FLT_MAX     :   ±%f\n",  FLT_MAX);
        printf("FLT_MAX     :   ±%E\n",  FLT_MAX);
        printf("FLT_MIN     :   %f\n",  FLT_MIN);
        printf("FLT_DIG     :   %d\n",  FLT_DIG);
        printf("\ndouble      :   %ld Byte\n", sizeof(double));
        //printf("DBL_MAX     :   %lf\n",  DBL_MAX);
        printf("DBL_MAX     :   ±%lG\n",  DBL_MAX);
        printf("DBL_MIN     :   %lf\n",  DBL_MIN);
        printf("DBL_DIG     :   %d\n",  FLT_DIG);
        printf("\nDatentyp long double: %ld Byte\n\n", sizeof(long double));
        printf("LDBL_MIN    :   %LG\n",  LDBL_MIN);
        printf("_DBL_MAX    :   %LG\n",  LDBL_MAX);    
        printf("LDBL_DIG    :   %d\n",  LDBL_DIG);
        
        return 0;
    }
    This lines are not shown by my post, no auto clicker matter why:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <float.h>
    For decimals with an absolute value greater than or equal to 1.0m, the value is exact to 28 or 29 digits. Contrary to the float and double data types, decimal fractional numbers such as 0.1 can be represented exactly in the decimal representation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 2
    Last Post: 05-23-2003, 02:46 PM
  3. "Any" type using "Boost"
    By Trauts in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2003, 10:53 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread