Click here to Skip to main content
15,888,454 members
Home / Discussions / C#
   

C#

 
RantWhy is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 2:21
Eric Lynch3-Mar-19 2:21 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Richard MacCutchan3-Mar-19 2:40
mveRichard MacCutchan3-Mar-19 2:40 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 4:24
Eric Lynch3-Mar-19 4:24 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Richard MacCutchan3-Mar-19 5:14
mveRichard MacCutchan3-Mar-19 5:14 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 5:27
Eric Lynch3-Mar-19 5:27 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Richard MacCutchan3-Mar-19 5:45
mveRichard MacCutchan3-Mar-19 5:45 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 6:55
Eric Lynch3-Mar-19 6:55 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 18:28
Eric Lynch3-Mar-19 18:28 
OK, this is sadly nearing obsessional Frown | :( I've gone through the effort of decoding every dang bit in the IEEE 754 formats.

Near as I can tell, the float->double conversion is doing absolutely what I would expect of it. Though, it is still yielding a result that has the appearance of being worse.

I'm currently baffled...maybe double.ToString() is the culprit? Maybe its something else entirely?

I'm going to give this a whole lot more thought tomorrow...at a decent hour. Though, since it has no practical impact on anything I'm actually doing, I should probably let it go. Regrettably, intellectual curiosity has a firm hold of me at this point Smile | :)

Below you'll find the output from my latest program, where I enter the text "123.456". "Single" / "Double" are conversions from the results of decimal.Parse. "Single (direct)" / "Double (direct)" are the results of float.Parse/double.Parse. The remainder are the indicated conversions of the results of a float.Parse.

Single: Sign=0, Exponent=6 (10000101), Significand=7793017 (11101101110100101111001)
  123.456
Single (direct): Sign=0, Exponent=6 (10000101), Significand=7793017 (11101101110100101111001)
  123.456
Double: Sign=0, Exponent=6 (10000000101), Significand=4183844053827191 (1110110111010010111100011010100111111011111001110111)
  123.456
Double (direct): Sign=0, Exponent=6 (10000000101), Significand=4183844144021504 (1110110111010010111100100000000000000000000000000000)
  123.456001281738
float->double: Sign=0, Exponent=6 (10000000101), Significand=4183844144021504 (1110110111010010111100100000000000000000000000000000)
  123.456001281738
float->decimal->double: Sign=0, Exponent=6 (10000000101), Significand=4183844053827191 (1110110111010010111100011010100111111011111001110111)
  123.456

After this much effort, I guess I'll eventually be forced to write an article on every useless bit of trivia I can find about all of these formats Smile | :)
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Peter_in_27803-Mar-19 3:23
professionalPeter_in_27803-Mar-19 3:23 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 4:58
Eric Lynch3-Mar-19 4:58 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Eric Lynch3-Mar-19 7:07
Eric Lynch3-Mar-19 7:07 
GeneralRe: Why is float to double conversion uniquely terrible? Pin
Richard Deeming5-Mar-19 2:15
mveRichard Deeming5-Mar-19 2:15 
QuestionC# how to get all files from a directory that is inside the current project Pin
dolfijn300027-Feb-19 10:26
dolfijn300027-Feb-19 10:26 
AnswerRe: C# how to get all files from a directory that is inside the current project PinPopular
Richard Deeming27-Feb-19 10:43
mveRichard Deeming27-Feb-19 10:43 
QuestionForce Interface Implementation in Derived Classes? Pin
pr1mem0ver27-Feb-19 6:51
pr1mem0ver27-Feb-19 6:51 
AnswerRe: Force Interface Implementation in Derived Classes? Pin
Richard Deeming27-Feb-19 7:51
mveRichard Deeming27-Feb-19 7:51 
GeneralRe: Force Interface Implementation in Derived Classes? Pin
pr1mem0ver28-Feb-19 1:25
pr1mem0ver28-Feb-19 1:25 
GeneralRe: Force Interface Implementation in Derived Classes? Pin
Richard Deeming28-Feb-19 1:36
mveRichard Deeming28-Feb-19 1:36 
AnswerRe: Force Interface Implementation in Derived Classes? Pin
Gerry Schmitz27-Feb-19 13:51
mveGerry Schmitz27-Feb-19 13:51 
GeneralRe: Force Interface Implementation in Derived Classes? Pin
pr1mem0ver28-Feb-19 1:41
pr1mem0ver28-Feb-19 1:41 
QuestionA problem with Lists / Arrays Pin
Nikol Dimitrova25-Feb-19 9:34
Nikol Dimitrova25-Feb-19 9:34 
AnswerRe: A problem with Lists / Arrays Pin
Richard Deeming25-Feb-19 10:56
mveRichard Deeming25-Feb-19 10:56 
Questionalign columns headers with cells values in a text file that exported from a datagridview in c# Pin
Member 1332584624-Feb-19 7:20
Member 1332584624-Feb-19 7:20 
AnswerRe: align columns headers with cells values in a text file that exported from a datagridview in c# Pin
Gerry Schmitz24-Feb-19 8:46
mveGerry Schmitz24-Feb-19 8:46 
GeneralRe: align columns headers with cells values in a text file that exported from a datagridview in c# Pin
Member 1332584624-Feb-19 13:23
Member 1332584624-Feb-19 13:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.