1

Explicit numeric conversion from float and double to any integral type described in the C# 5.0 specification (paragraph 6.2.1) as follows:

• For a conversion from float or double to an integral type, the processing depends on the overflow checking context (§7.6.12) in which the conversion takes place:

  o In a checked context, the conversion proceeds as follows:
    • If the value of the operand is NaN or infinite, a System.OverflowException is thrown.
    • Otherwise, the source operand is rounded towards zero to the nearest integral value. 
      If this integral value is within the range of the destination type then 
      this value is the result of the conversion.
    • Otherwise, a System.OverflowException is thrown.
  o In an unchecked context, the conversion always succeeds, and proceeds as follows.
    • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.
    • Otherwise, the source operand is rounded towards zero to the nearest integral value.
      If this integral value is within the range of the destination type then
      this value is the result of the conversion.
    • Otherwise, the result of the conversion is an unspecified value of the destination type.

At the same time rules for same conversion described in the MSDN as follows:

When you convert from a double or float value to an integral type, the value is truncated. If the resulting integral value is outside the range of the destination value, the result depends on the overflow checking context. In a checked context, an OverflowException is thrown, while in an unchecked context, the result is an unspecified value of the destination type.

Evaluating of such conversion, for example "(int)123.566", gets us "123". Is description given in the specification correct?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tenere
  • 361
  • 5
  • 11
  • What are you confused about? The example you give is consistent with what's described in the specification. 123.566 gets rounded down to 123, which is within the range of int, so the result is the int 123. – Craig Gidney Dec 29 '13 at 10:19
  • 1
    The only potential for confusion I see is "rounded towards zero" vs. "truncated", but they are merely two different ways of describing the same operation. –  Dec 29 '13 at 10:23
  • @Strilanc, thank you. I haven't noticed "towards zero". – Tenere Dec 29 '13 at 10:24

2 Answers2

4

Both descriptions in MSDN and C# 5.0 Specification are correct.

C# expressions are unchecked by default. Take a look at the bold part again;

  • In an unchecked context, the conversion always succeeds, and proceeds as follows.
    • If the value of the operand is NaN or infinite, the result of the conversion is an unspecified value of the destination type.
    • Otherwise, the source operand is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type then this value is the result of the conversion.

First, let's take a look what Specification says:

rounded towards zero to the nearest integral value

Let's analyze it on number line;

enter image description here

As we can see, result will be 123 when we round towards zero.

Second, let's take a look what MSDN says:

When you convert from a double or float value to an integral type, the value is truncated

From Wikipedia page;

In mathematics and computer science, truncation is the term for limiting the number of digits right of the decimal point.

Note that in some cases, truncating would yield the same result as rounding, but truncation does not round up or round down the digits; it merely cuts off at the specified digit.

As we can see, result will be 123 when we truncate it also.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    I believe that in C#, expressions are by default unchecked. Of course, you could change that in your IDE. Not that it matters for this case, as also in an unchecked context, the spec says "the source operand is rounded towards zero to the nearest integral value." – Kris Vandermotten Dec 29 '13 at 10:31
  • @KrisVandermotten Yeap. I feel I need a coffee right now. Updated my answer. – Soner Gönül Dec 29 '13 at 11:00
2

Yes, it is.

(I'm sorry that this is not an long and qualified answer, but you did not ask for anything else.)

nvoigt
  • 75,013
  • 26
  • 93
  • 142