Fun with double
-
I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D
static void Main(string[] args)
{
var e = 1e18;
Console.WriteLine(IsEven(e));
Console.WriteLine(IsEven(e + 1));
Console.WriteLine(IsEven(-e));
Console.WriteLine(IsEven(-e + 1));
}
bool IsEven(double d)
{
if (d < 0) return (d % 2) > -1;
return (d % 2) < 1;
}A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
What makes this even nicer is that both 1.0e+18 and 1.0 are exactly representable as a double, but their sum isn't. A nice demonstration of one of the pitfalls of floating-point arithmetic.
Ad astra - both ways!
-
What makes this even nicer is that both 1.0e+18 and 1.0 are exactly representable as a double, but their sum isn't. A nice demonstration of one of the pitfalls of floating-point arithmetic.
Ad astra - both ways!
ha, yeah, it's better than summing up 0.1 10 times, since 0.1 is an approximation to start with... :-D
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D
static void Main(string[] args)
{
var e = 1e18;
Console.WriteLine(IsEven(e));
Console.WriteLine(IsEven(e + 1));
Console.WriteLine(IsEven(-e));
Console.WriteLine(IsEven(-e + 1));
}
bool IsEven(double d)
{
if (d < 0) return (d % 2) > -1;
return (d % 2) < 1;
}A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
When you start with approximations... You should conclude with "apporximately correct" = "correct" There are ONLY Zeroes and Ones... Everything else is up for interpretation!
-
When you start with approximations... You should conclude with "apporximately correct" = "correct" There are ONLY Zeroes and Ones... Everything else is up for interpretation!
01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
No finer compliment to a response has EVER been paid... Thank You!
-
01001100 01101001 01101011 01100101 00101100 00100000 01110100 01101111 01110100 01100001 01101100 01101100 01111001 00100000 01101101 01100001 01101110 00100001
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
I got : L Ei then I got bored and gave up. :rolleyes: Oh, I can't believe this is out there: SPOILER Binary to Ascii Text Converter[^] I pasted it in and converted.
Damn, I had to quickly write a program to do that, if only I had known! :rolleyes: :laugh:
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
Damn, I had to quickly write a program to do that, if only I had known! :rolleyes: :laugh:
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
I must be old! I read that fairly quickly. Years spent eyeball reading punched paper tape. ASCII brain still works; I think I've lost baudot. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
I am doing a lot of computational maths work at home at the moment and, just for giggle, I thought of writing the following method. Code behave as incorrectly as expected! :-D
static void Main(string[] args)
{
var e = 1e18;
Console.WriteLine(IsEven(e));
Console.WriteLine(IsEven(e + 1));
Console.WriteLine(IsEven(-e));
Console.WriteLine(IsEven(-e + 1));
}
bool IsEven(double d)
{
if (d < 0) return (d % 2) > -1;
return (d % 2) < 1;
}A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
Either use
Math.Round
, or convert the value to adecimal
type before performing math, and then back to adouble
after performing math.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Either use
Math.Round
, or convert the value to adecimal
type before performing math, and then back to adouble
after performing math.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013lol initially I was wondering what was your point.. but I realise now it's subtle sarcasm, got me, hahah! :laugh:
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
lol initially I was wondering what was your point.. but I realise now it's subtle sarcasm, got me, hahah! :laugh:
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
:)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013