Skip to main content
gamedev.net
🔒 Locked

[.net] float data type

Started by sevak 2 replies 850+ views
Original Post
sevak
sevak
Isn't the float data type only capable of storing up to 7 significant digits? Thats what my C# books says. However, if you enter 4 in this program you get much more than just 7 significant digits.

using System;

class Circle
{
  static void Main()
  {
    Console.Write("Enter the radius of a circle: ");
    
    float fradius = Single.Parse(Console.ReadLine());
    
    Console.WriteLine("The area of the circle is " + (Math.Pow(fradius,2) * Math.PI) );
  }
}




www.computertutorials.org
computertutorials.org-all your computer needs...
tHiSiSbOb
tHiSiSbOb
It may mean with a certain level of accuracy...
----------------------------------------------------"Plant a tree. Remove a Bush" -A bumper sticker I saw.
Drew_Benton
Drew_Benton
Quote:

The float type can represent values ranging from approximately 1.5 × 10−45 to 3.4 × 1038 with a precision of 7 digits.

The double type can represent values ranging from approximately 5.0 × 10−324 to 1.7 × 10308 with a precision of 15-16 digits.


Straight from MSDN. I do not know why you are getting more than 7 in C# - it would have to do something with the output. Try setting a breakpoint on the Console.WriteLine and see the value of fradius.
sevak
sevak
thx I'm so stupid I didn't see what I was doing wrong.

(Math.Pow(fradius,2) * Math.PI)

Math.Pow() returns a double and Math.PI is a double const so the multiplication results in a double which holds 15-16 digits.

www.computertutorials.org
computertutorials.org-all your computer needs...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.