c# - Why isn't conversion of Decimal.MaxValue between Single and Decimal transitive? -


if try , convert decimal.maxvalue decimal single , again, conversion fails overflowexception:

convert.todecimal(convert.tosingle(decimal.maxvalue)) // '...' threw exception of type 'system.overflowexception' //     base: {"value either large or small decimal."} 

what gives? surely value of decimal.maxvalue single should valid decimal value?

i understand differences between single , decimal , expect loss of precision converting decimal single single.maxvalue greater decimal.maxvalue doesn't make sense "value either large or small decimal". if think make sense please explain why in answer.

additionally,

convert.tosingle(decimal.maxvalue) // 7.92281625e+28 

so there no problem converting number single.

you're making incorrect assumption:

surely value of decimal.maxvalue single should valid decimal value?

the value of decimal.maxvalue 79,228,162,514,264,337,593,543,950,335. float can't represent anywhere near level of precision, when convert decimal value float end approximation.

in case float represented internally 2^96. turns out pretty approximation -- 79,228,162,514,264,337,593,543,950,336 -- take note of last digit.

the value of float larger decimal.maxvalue, why attempt convert decimal fails.

(the .net framework doesn't offer when trying diagnose these kind of problems. it'll display pre-rounded value float -- 7.92281625e+28 or similar -- , never full approximation it's using internally.)


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -