Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the result of Decimal.MaxValue - 0.5 an integer?

Tags:

c#

.net

decimal

I was playing with decimal today. I noticed this:

Decimal.MaxValue 
79228162514264337593543950335

Decimal.MaxValue - 0.5m
79228162514264337593543950334

The following code prints true.

static void Main(string[] args)
        {
            decimal d = Decimal.MaxValue - 0.5M;
            var b = d % 1 == 0;
            Console.WriteLine(b);

        }

I am sure there is a reason behind this but I don't know what it is.

like image 703
chikak Avatar asked Aug 17 '10 09:08

chikak


People also ask

What is decimal MaxValue?

Decimal MaxValue represents the largest possible value of Decimal. This field is constant and read-only.

Is decimal a value type?

The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding.

Can an int be a decimal C#?

Numbers in C# Numbers, in general, can be divided into two types: Integer type and floating-point types. Integer type numbers are whole numbers without decimal points. It can be negative or positive numbers.

How to initialize a n with value integer max_value + 1?

Trying to initialize a N with value Integer.MAX_VALUE + 1 N = -2147483648. Integer.MIN_VALUE. Integer.MIN_VALUE is a constant in the Integer class of java.lang package that specifies that stores the minimum possible value for any integer variable in Java. The actual value of this is.

Is 0 5 an integer or a decimal?

But 0.5 cannot be represented as a decimal of the required precision and is rounded upwards to 1. Show activity on this post. It is not an integer, it is in fact a Decimal. Integers (Int32) can have values from negative 2,147,483,648 positive 2,147,483,647. As you can see, this is well out of these ranges.

Why is the value of an integer always equal to min_value?

Because the integer overflows. When it overflows, the next value is Integer.MIN_VALUE. If an integer addition overflows, then the result is the low-order bits of the mathematical sum as represented in some sufficiently large two's-complement format.

What is Max integer value in Java?

Integer.MAX_VALUE is a constant in the Integer class of java.lang package that specifies that stores the maximum possible value for any integer variable in Java. The actual value of this is. Any integer variable cannot store any value beyond this limit.


2 Answers

The decimal type uses 96 bits to store the sequence of digits (ref), plus a sign (1 bit) and a scaling factor that specifies the location of the decimal place.

For this decimal number:

79228162514264337593543950335

All 96 bits are used to the left of the decimal point - there's nothing left to represent the fractional part of the answer. So, it gets rounded.

If you divide the number by 10:

7922816251426433759354395033.5

Then you have a few bits available to represent the fractional part - but only to 1/10, no finer.

The key difference between decimal and double/float is that it is based on a decimal scaling factor specifying the location of a decimal point; the other floating types are based on a binary scaling factor specifying the location of a binary point.

like image 147
Bevan Avatar answered Oct 22 '22 05:10

Bevan


0.5 is being rounded before subtraction. decimal strives to make the result as precise as possible, so the operation becomes 79228162514264337593543950335 - 00000000000000000000000000000.5. But 0.5 cannot be represented as a decimal of the required precision and is rounded upwards to 1.

like image 22
Anton Tykhyy Avatar answered Oct 22 '22 05:10

Anton Tykhyy