Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:10:56+00:00 2026-05-27T09:10:56+00:00

Why does the compiler decide that 2.3 is double so this code won’t compile:

  • 0

Why does the compiler decide that 2.3 is double so this code won’t compile:

decimal x;
x = 2.3; // Compilation error - can not convert double to decimal.
x = (decimal) 2.3 // O.k.

Why the compiler doesn’t think like this:
He wants to get a decimal, he give me a value that can be decimal, So it’s decimal!

And why this doesn’t get compilation error:

short x;
x = 23; // O.K.

Who said that 23 isn’t an int?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:10:56+00:00Added an answer on May 27, 2026 at 9:10 am

    There are a lot of questions here. Let’s break them down into small questions.

    Why is the literal 2.3 of type double rather than decimal?

    Historical reasons. C# is designed to be a member of the “C-like syntax” family of languages, so that its superficial appearance and basic idioms are familiar to programmers who use C-like languages. In almost all of those languages, floating point literals are treated as binary not decimal floats because that’s how C did it originally.

    Were I designing a new language from scratch I would likely make ambiguous literals illegal; every floating point literal would have to be unambigiuously double, single or decimal, and so on.

    Why is it illegal in general to convert implicitly between double and decimal?

    Because doing so is probably a mistake, in two ways.

    First, doubles and decimals have different ranges and different amounts of “representation error” — that is, how different is the quantity actually represented from the precise mathematical quantity you wish to represent. Converting a double to a decimal or vice versa is a dangerous thing to do and you should be sure that you are doing it correctly; making you spell out the cast calls attention to the fact that you are potentially losing precision or magnitude.

    Second, doubles and decimals have very different usages. Doubles are usually used for scientific calculations where a difference between 1.000000000001 and 0.99999999999 is far smaller than experimental error. Accruing small representation errors is irrelevant. Decimals are usually used for exact financial calculations that need to be perfectly accurate to the penny. Mixing the two accidentally seems dangerous.

    There are times when you have to do so; for example, it is easier to work out “exponential” problems like mortgage amortization or compounded interest accrual in doubles. In those cases again we make you spell out that you are converting from double to decimal in order to make it very clear that this is a point in the program where precision or magnitude losses might occur if you haven’t gotten it right.

    Why is it illegal to convert a double literal to a decimal literal? Why not just pretend that it was a decimal literal?

    C# is not a “hide your mistakes for you” kind of language. It is a “tell you about your mistakes so you can fix them” kind of language. If you meant to say “2.3m” and you forgot the “m” then the compiler should tell you about it.

    Then why is it legal to convert an integer literal (or any integer constant) to short, byte, and so on?

    Because an integer constant can be checked to see if it is in the correct range at compile time. And a conversion from an in-range integer to a smaller integral type is always exact; it never loses precision or magnitude, unlike double/decimal conversions. Also, integer constant arithmetic is always done in a “checked” context unless you override that with an unchecked block, so there is not even the danger of overflow.

    And it is less likely that integer/short arithmetic crosses a “domain” boundary like double/decimal arithmetic. Double arithmetic is likely to be scientific, decimal arithmetic is likely to be financial. But integer and short arithmetic are not each clearly tied to different business domains.

    And making it legal means that you don’t have to write ugly unnecessary code that casts constants to the right types.

    There is therefore no good reason to make it illegal, and good reasons to make it legal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does the compiler optimize out any multiplications by 1? That is, consider: int a
The Visual Studio compiler does not seem to warn on signed/unsigned assignments, only on
For instance, does the compiler know to translate string s = test + this
I was wondering if there is a possible optimization where the compiler does not
In forward referencing language such as c#, how does the compiler handle this? What
What does a JIT compiler specifically do as opposed to a non-JIT compiler? Can
I decided to modify some code today, and encountered an error that doesn't make
I have the problem that C++ compiler does a recompile without any changes done.
How does the C++ compiler decide which function/method to call if there are multiple
I am led to believe that the java compiler does all the work of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.