Ok, I thought I was clear on the double, int, float, decimal etc types. I used typed variables in VB and was comfortable with them. I've just started C# and out of the gate I feel totally clueless. I keep assigning double type variables expressions with division and when I out put the value the decimal portion is chopped off and I get a whole number. Here's my question for the following I get:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            double x = 4/12;
            double y = 7/12;
            double z = x/y;
            Console.Write("z=" + z);
            Console.Write("\n");
            Console.Write("x=" + x);
            Console.Write("\n");
            Console.Write("y="+ y);


            Console.Read();
        
        }
    }
}

z=NAN
x=0
y=0

Or if the number is larger and I use division the answer truncates the decimal and I get a whole number EX:

double example = 12 + (2/12)

this gives me 12 as a result, could someone please explain, I'm sorry for such a simple question, but I'm at a loss. I thought doubles were meant to hold numbers with a floating decimal point.

Thanks for any help : )

You know, I have done so much of C# , but never came across this paradox. Thanks, For letting me know.

After my research, all I can say is when you use fractions in your coding (i.e 2/5 or 5/7) the debugger cannot understand you, but if you use decimal numbers such as .5 , 4.6 and 3.945 the code works perfectly.

Apparently, There is a Fraction class in C# .net which deals with the use os fraction numbers. For more information ,check this out :
http://www.codeproject.com/KB/recipes/fractiion.aspx

Hope it Helps,
Happy Coding

You know, I have done so much of C# , but never came across this paradox. Thanks, For letting me know.

After my research, all I can say is when you use fractions in your coding (i.e 2/5 or 5/7) the debugger cannot understand you, but if you use decimal numbers such as .5 , 4.6 and 3.945 the code works perfectly.

Apparently, There is a Fraction class in C# .net which deals with the use os fraction numbers. For more information ,check this out :
http://www.codeproject.com/KB/recipes/fractiion.aspx

Hope it Helps,
Happy Coding

No this does indeed help! Thanks so much. I've been playing with it and noticed that when I declare the pieces separately as integers then I perform the division and store the results in a double it works, also when I type cast all the numbers as doubles ot seems to work as well:

double x = (double)2/(double)12;
            double y = (double)7 / (double)12;
            double z = (double)x / (double)y;
            Console.Write("x=" + x);
            Console.Write("\n");
           
            Console.Write("y="+ y);
            Console.Write("\n");
            Console.Write("z=" +z);

Thanks again I'll check the link!

The runtime is doing integer math on your division and then casting the result to a double afterwards. As such, 2 / 12 in integer math will equal 0, and then that's what your double will store. You've already seen you can get the result you expect by casting everything to double, but you don't have to do that unless you are working with integer variables (and you really only need to do it for one side of the equation, the runtime will take care of everything else). If you have numbers hardcoded into your code, any of the following statements will achieve your desired result.

double temp1 = 2d / 12;
double temp2 = 2.0 / 12;
double temp3 = 2 / 12d;
double temp4 = 2 / 12.0;
double temp5 = 2d / 12d;
double temp6 = 2d / 12.0;
double temp7 = 2.0 / 12d;
double temp8 = 2.0 / 12.0;

The point is that numeric literals are assumed to be integers if they are whole numbers and doubles if they are decimals. If you do math with numbers before assigning them to variables, they will default to their native types if you provide no additional information for the compiler.

1 = integer 1
1f, 1.0f = floating point 1
1d, 1.0 = double 1
1m, 1.0m = decimal 1

Edited 6 Years Ago by apegram: n/a

Comments
Welcome to Daniweb. That was a very well written explaination of how the compiler treats hardcoded numbers :)
Very good explanation!
very helpful

Fantastic! That's what I was beginning to see the more I played with it, thanks for spelling it out so well.

This article has been dead for over six months. Start a new discussion instead.