Compute the double value nearest preferred decimal result

前端 未结 3 1196
渐次进展
渐次进展 2021-01-21 19:28

Let N(x) be the value of the decimal numeral with the fewest significant digits such that x is the double value nearest the value of the numeral.

Given

3条回答
  •  既然无缘
    2021-01-21 19:45

    You could also inquire in Smalltalk Pharo 2.0 where your request translates:

    ^(b asMinimalDecimalFraction - a asMinimalDecimalFraction) asFloat
    

    Code could be found as attachment to issue 4957 at code.google.com/p/pharo/issues - alas, dead link, and the new bugtracker requires a login...

    https://pharo.fogbugz.com/f/cases/5000/Let-asScaledDecimal-use-the-right-number-of-decimals

    source code is also on github, currently:

    https://github.com/pharo-project/pharo-core/blob/6.0/Kernel.package/Float.class/instance/printing/asMinimalDecimalFraction.st

    The algorithm is based on:

    Robert G. Burger and R. Kent Dybvig
    Printing Floating Point Numbers Quickly and Accurately
    ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation
    June 1996.
    http://www.cs.indiana.edu/~dyb/pubs/FP-Printing-PLDI96.pdf

提交回复
热议问题