Module | AppMath::Basics |
In: |
appmath_basics.rb
|
Returns the nearest number to x, which up to sign and power of 10 is one of the numbers in the array c in the function body. For instance,
c = [1,2,5,7.5,10]
In a sense, here cutting is a more drastic version of rounding.
# File appmath_basics.rb, line 68 68: def Basics.cut(x) 69: fail "x is not a number" if x.nan? 70: fail "x is not finite" if x.infinite? 71: c = [R.c1,R.c2,R.c5,R.c(7.5),R.c10] 72: return R.c0 if x.zero? 73: s = Basics.sign(x) 74: y1 = x.abs 75: ylog = y1.log10 76: ylogInt = ylog.floor 77: yMantisse = ylog - ylogInt 78: y = R.c10 ** yMantisse 79: fail "Basics.cut(): !(1 <= y)" if !(R.c1 <= y) 80: fail "Basics.cut(): !(y < 10)" if !(y < R.c10) 81: i = 0 82: while y > c[i] 83: i += 1 84: end 85: cd = c.length 86: fail "unexpected failure in function Basics.cut" if i < 0 || i >= cd 87: yu = c[i] # yu is the smallest c[k] which is larger or equal to y 88: if i==0 89: yf = yu 90: else 91: fail "unexpected failure in function Basics.cut" if (i-1) < 0 || (i-1) >= cd 92: yl = c[i-1] 93: yf = yu-y <y -yl ? yu : yl # now yf is the c[k] which is nearest to y 94: end 95: return yf.ldexp(ylogInt) * s # sign and exponent are restored 96: end
Returns the smaller of the two numbers a and b.
# File appmath_basics.rb, line 42 42: def Basics.inf(a, b) 43: a < b ? a : b 44: end