c# - 64 vs 32 bit double parsing issue with the round-trip format specifier "R" -


i've got scenario in .net (4.6.1) parsing string representation of 6dp floating point value produces different results in 32 , 64 bit mode.

[fact] public void parsetest() {     var numtext = "51.580133";     double.parse(numtext)         .tostring("r")         .should().be(numtext); } 

this test passes in 32 bit mode, 64 bit mode fails text generated is: "51.580132999999996"

i'd expect rounding issues irrational numbers or numbers derived via equation, there no ambiguity length , accuracy of floating point here.

this inside older system changing decimal take significant effort.

questions:

  1. why happen?
  2. what options there reliably round / truncate value 6dp?

update works, , produces different output tostring("g6"):

[fact] public void parsetext() {     var numtext = "51.580133";     double.parse(numtext)         .tostring("g8")         .should().be(numtext); } 

i found interesting point microsoft might explain issue

in cases, double values formatted "r" standard numeric format string not round-trip if compiled using /platform:x64 or /platform:anycpu switches , run on 64-bit systems.

to work around problem, can format double values using "g17" standard numeric format string. following example uses "r" format string double value not round-trip successfully, , uses "g17" format string round-trip original value.

the comment , example can found here: https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -