This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between Decimal, Float and Double in C#?
Today I'm wondering about Double in .Net. I've used it with Int32 the past days and started wondering what the max value is.
The MSDN page for Double.MaxValue says 1.7976931348623157E+308. I'm pretty sure I'm reading that wrong.
How many bytes does Double take up (in memory)?
What is the actual maximum number (explain the the E+308)?
Is Double.MaxValue bigger than UInt32? Bigger than UInt64?
And while we are at it, what is the difference between Float and Double?
Basically,
Double is 64 bit floating point value and float is a 32 bit.
So double is able to store twice big value as of float.
http://msdn.microsoft.com/en-us/library/678hzkk9(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/b1e65aza(v=vs.71).aspx
Just read the top lines on the links, you'll get an idea.
About E+308: though 2^64 is far less that 1e+308, you must consider that double is not "precise" number, it has only a few significant digits (precision), so it does not need to store all ~308 digits. With this logic behind the double structure, it can contain numbers up to e+308 in 64 bits.
Related
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 3 years ago.
I am grabbing total RAM of a computer system and available RAM and trying to work out what percentage is available.
I am using the following code:
double percent = my.Info.AvailablePhysicalMemory / my.Info.TotalPhysicalMemory;
and have also tried:
decimal percent = my.Info.AvailablePhysicalMemory / my.Info.TotalPhysicalMemory;
I am sure it's an issue with the type but I am unsure why both methods give a result of 0.
The actual values are Total: 17072574464 and Available: 8746000384. The values come back from the system cast as ulong. So what does percent always equal 0? If I put the numbers in directly it works fine. Just can't use the ulong variables - hence why I am sure it's my lack of experience with types in C# that is the problem.
You are trying to divide an integer by an integer, which always rounds down. You need to convert to a floating point number before you divide; for example:
double percent = my.Info.AvailablePhysicalMemory * 1.0 / my.Info.TotalPhysicalMemory;
This question already has answers here:
Difference between decimal, float and double in .NET?
(18 answers)
Closed 3 years ago.
I really dont unterstand why, but my double array just sometimes round my variables, even though it shouldn't. The weird thing is, that it only does this sometimes, as you see in the picture. All of the array elements should have this long precision. The Variable "WertunterschiedPerSec" is also at this precision every time, but still, if i add it to Zwischenwerte[i], then it sometimes just get less precisie, even though i dont do anything anywhere. Does anybody know why?
I would suggest using a decimal, but let's get into the exact details:
double, float and decimal are all floating point.
The difference is double and float are base 2 and decimal is base 10.
Base 2 numbers cannot accurately represent all base 10 numbers.
This is why you're seeing what appears to be "rounding".
I would use the decimal variable instead of double because you can basically do the same functions as a double except for using the Math function. Try to use Decimals and if you need to convert than use:
Double variable = Convert.ToDouble(decimal var);
Decimals are meant for decimals so they will hold more information than a float or decimal
This question already has answers here:
Is double Multiplication Broken in .NET? [duplicate]
(6 answers)
Closed 6 years ago.
my unit test failed because of this, so I wonder if I am using the right datatype?
Reading the specs of the double I'd think it should be ok, but this is what's happening:
I'm reading a string from a file with value 0,0175 (comma as decimal sep.)
then I convert it to a double and then multiply it by 10000.
The function which does the multiply is this:
private static double? MultiplyBy10000(double? input)
{
if (!input.HasValue)
{
return null;
}
return input.Value*10000;
}
next is from the immediate window:
input.Value
0.0175
input.Value*10000
175.00000000000003
And that is where my unit test fails, because I expect 175.
Is the double not accurate enough for this?
Checked other values too:
input.Value*1
0.0175
input.Value*10
0.17500000000000002
input.Value*100
1.7500000000000002
input.Value*1000
17.5
input.Value*10000
175.00000000000003
The weird thing is, I have 12 testcases
0,0155
0,0225
0,016
0,0175
0,0095
0,016
0,016
0,0225
0,0235
0,0265
and assert 4 of these, and the other 3 don't have this behaviour
Is the double not accurate enough for this?
No, doubles are floating point numbers, which are inaccurate by design. You can use decimal which is accurate and more suitable if you need exact numbers.
Floating point numbers where the value to the right of the decimal point are not powers of 2 cannot be accurately represented. Although you've got what looks like 0.0175 there's actually more data there.
After you've scaled the number up use Math.Round to trim off date on the right off the decimal point.
This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
Closed 7 years ago.
In my computer when I harcode a variable with 0.3(or maybe some other value) of value and I debug and check the variable's value , its value is 0.29999992 but in my friends computer, it stays in 0.3.
//stores 0.29999992
double variable= 0.3;
is there a configuration problem or something related?
Thanks
This is just an artifact of how binary floating-point works. There is no way of accurately representing 0.3 in a double (or a float for that matter). If you need that (e.g. for monetary applications), use decimal instead.
Welcome to the world of floating point numbers. Some, seemingly innocuous numbers cannot be represented exactly in floating point notation. A very close approximation is used instead.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.Net float to double conversion
I though I understood floating point but can someone explain the following
float f = 1.85847987E+9F;
double d = Convert.ToDouble(f);
d is now converted to a string as 1858479872.0. I'm assuming the extra 2 is because double cannot represent the floating point number exactly.
My question is why does it seem to be able to rerepsent the same number when assigned directly
double d = 1.85847987E+9;
and it is shown exactly as 185847987.0
Because double can, and float cannot represent 1.85847987E+9 precisely.
Why the compiler doesn't complains about "float f = 1.85847987E+9F;" if it cant represents it properly
As per C# specification, section 4.1.6 Floating point types
The floating-point operators, including the assignment operators, never produce exceptions.
The problem is not double but float. Float is limited to 32 bit, while double uses 64 bit for precision.
Because a float exists out of a sign, mantissa and exponent. See Jon Skeet's answer here how to extract those values.
For 1.85847987E+9F, the mantissa is 7259687 and the exponent is 8. Then mantissa << exponent equals 1858479872. Since the precision for a float is limited to 7 digits, the value of any digit beyond 7 digits depends on the implementation, not the input. You can test this easily by inputting 123456789F.