About the data type FLOAT

This is a very simple pgm where i hav been tryin to divide numbers.Am confused about this.Suppose i divide a number by zero the control has to go to the exception block and display the message,but that does not happen,I get the result from Console.Writeline as:

"The ans is:Infinity

How s tis possible when i use the datatype float?I wud like to kno wats happening behind?The result is the same even when i dont use Exception.

using System;

using System.Collections.Generic;

using System.Text;

namespace floatchk

{

classProgram

{

staticvoid Main(string[] args)

{

Program p =newProgram();

p.vasumathy();

}

publicvoid vasumathy()

{

float a, b;

try

{

Console.WriteLine("Enter the num to be divided");

a =Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the divisor");

b =Convert.ToInt32(Console.ReadLine());

float c = (float)(a / b);

Console.WriteLine("The answer is:"+c);

}

catch (DivideByZeroException e)

{

Console.WriteLine("Sorry u cannot divide a number by zero");

}

catch (Exception e)

{

Console.WriteLine(e);

}

}

}

}

Thanks in advance.

[2385 byte] By [vasumathy] at [2008-1-5]
# 1
Yes for float numbers like float an double, they don't throw an exception on division by 0, they return (+/-)inifinity and for 0/0 -->NaN, they work in a smart way.
Anyway, you could just check for the result if it is infinity by
float.isInfinity(float f)
or float.isPositifinfinity(f) ...
or float.isNaN(f)

Using checking is always better when it is available than throwing and catching exceptions(they're much slower)
Hope this helped

bruno_1 at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

Hi,

I tried same code snippet. I got the Exception "Attempted to Divide by zero".

Even if I check whether infinity, float.IsInfinity(), it always got the above exception.

I don't understand how come it's possible in your case?

Do you find any proper reason for that?

soujanya at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
I tried the same program, and no exception for me. What version of .Net are you using ? (I think it don't matter, but anyway there should be something missing)
bruno_1 at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
i'm using VisualStudio 2005.
soujanya at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
Me too, i think you should have .net 2.0 framework. There should something wrong, dividing by 0 will be thrown only when you divide integers or decimals, and not float types (float, double).
bruno_1 at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 6

hi..

'a' and 'b' in this case are Integers and the result is casted into float.

does that make any difference?

soujanya at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 7

float a, b;

Here, a and b are declared float, this is what matter.


Convert.toInt32(string) just parse the string, you could use int.Parse(string), because the integer is implicitly casted to a double after "a = Convert.toInt32(string) ". and finally we have a/b, the result will be float, here you will not get an exception whatever u put in a or b.

Maybe you changed a little bit in the program,and declared a and b as integer, that explain why you are getting an exception ?

bruno_1 at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 8
Hi soujanya, see if these examples help.

int d = 0;
int n = 5;

float v = (float) (n / d) <-- Throws exception
float v = (float)n / d <-- Does not throw exception
float v = ((float) n) / d <-- Does not throw exception

In the first case, the two numbers are still of type int when the division happens, and thus, you will get an exception. In the second and third case n is converted to a float first, and then the division happens, so you won't get an exception.

Pace at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 9

Hi Pace...

Now it's clear for me. Earlier, I took both the numbers of Int32 type and so the exception for me.

Anyways...thanks for your clarification.

soujanya at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...