CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2011
    Posts
    2

    novice c# question

    I wrote a program that converts Celsius to Fahrenheit and vice versa. when converting Celsius to Fahrenheit I used the formula (9/5) * Celsius + 32. but that didn't work, I discovered that 9/5 was returning 1 rather than 1.8 and I'm not sure why that is. I fixed the problem by just using the formula: 1.8 * Celsius + 32, but I don't understand why I had to. The Fahrenheit to Celsius bit works fine.

    My code :


    double dubCelsius;
    double dubFarenheit;

    if (rbtnFar.Checked)
    {
    double.TryParse(txtInput.Text, out dubFarenheit);
    dubCelsius = (dubFarenheit - 32) * 5 / 9;
    txtResult.Text = dubCelsius.ToString();
    }

    if (rbtnCel.Checked)
    {
    double.TryParse(txtInput.Text, out dubCelsius);
    dubFarenheit = 1.8 * dubCelsius + 32;
    txtResult.Text = dubFarenheit.ToString();
    }

    I have a feeling the answer to this is going to be very obvious but I just cant figure it out.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: novice c# question

    It is happening because you are performing integer division. You have to pay attention to your data types. To fix it, use

    Code:
    9.0/5
    That returns a floating point number. Further reading: http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx

  3. #3
    Join Date
    Aug 2011
    Posts
    2

    Re: novice c# question

    thank you very much.

  4. #4
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: novice c# question

    I believe the 9/5 result of your calculation is being used as an int... try

    Code:
    dubCelsius = (dubFarenheit - 32) * ((double)9/5);
    Last edited by fcronin; August 24th, 2011 at 01:52 PM. Reason: Corrected by BigEd...

  5. #5
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: novice c# question

    slow draw...

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: novice c# question

    Actually, (double)(9/5) has the same problem. The integer division will be performed first, and then the result will be cast to a double. If you want to cast you would need to cast one operand first, i.e., ((double)9/5)

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: novice c# question

    In VB / is for decimal division and \ is for int division

    10/4 = 2.5
    10\4 = 2

    5.423/1 = 5.423
    5.423\1 = 5

    odd that C# would return an int using / in this case.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: novice c# question

    Quote Originally Posted by DataMiser View Post
    In VB / is for decimal division and \ is for int division

    10/4 = 2.5
    10\4 = 2

    5.423/1 = 5.423
    5.423\1 = 5

    odd that C# would return an int using / in this case.
    I would say it's odd that VB would introduce a separate division operator which occludes the fact that you are performing integer division, but of course, this is $^@%ing VB we're talking about... C# only has a single operator, they don't like to pollute the syntax unnecessarily as VB is wont to do.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: novice c# question

    My point was that the basic / symbol does normal division like one would expect and and you get an int result only if you use the alternate symbol. I would expect C# to behave like VB does in the case of / giving a decimal result.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jan 2010
    Posts
    1,133

    Re: novice c# question

    It's a matter of language design. IIRC, Pascal, for example, has div for... one of those, and / for the other (see the downside: after a while, I can't remember which one is which.)

    C# took this concept from the C-family of languages (C++, Java). At first, it may seem counter-intuitive, but after a while it really makes sense - the language performs an integer division if only integers are involved, and if not, it decides for floating-point division and returns a result of the appropriate type, trying to retain the original precision (for example: float if, besides possible integers, only floats are involved, and double if there is a double operand).

    So basically, if you know the type of your operands, you know what operation will be performed, and what will be the type of the result.

    This is why C# (but also C++) programmers write things like:
    Code:
    1.0 / 3.0              // these are treated as doubles
    1.0f / 3.0f           // f is a suffix that denotes the value as a float literal
    1 / 3.0f              // one operand is float, so floating-point division, and float result
    
    // results in a double (Count is integer, cast to double; numDivisions is an integer)
    (double)list.Count / numDivisions
    Last edited by TheGreatCthulhu; August 25th, 2011 at 11:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured