Module AppMath::Basics
In: appmath_basics.rb

A Module which collects simple utility functions.

Methods

cut   inf   sign   sign2   sup  

Public Class methods

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.

[Source]

    # 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.

[Source]

    # File appmath_basics.rb, line 42
42: def Basics.inf(a, b)
43:   a < b ? a : b
44: end

Returns the sign of a as a real number.

[Source]

    # File appmath_basics.rb, line 47
47: def Basics.sign(a)
48:   if a > R.c0
49:     R.c1
50:   elsif a < R.c0
51:     -R.c1
52:   else
53:     R.c0
54:   end
55: end

Returns ‘a with the sign of b’. Programming idiom frequently used in the ‘Numerical Recipes in C’ by Press et al.

[Source]

    # File appmath_basics.rb, line 59
59: def Basics.sign2(a, b)
60:   b >= R.c0 ? a.abs : -a.abs
61: end

Returns the larger of the two numbers a and b.

[Source]

    # File appmath_basics.rb, line 37
37: def Basics.sup(a, b)
38:   a <= b ? b : a
39: end

[Validate]