Module: Sevgi::Function::Math
- Included in:
- Sevgi::Function
- Defined in:
- lib/sevgi/function/math.rb
Overview
Numeric and trigonometric helpers used by geometry and DSL code.
Constant Summary collapse
- PRECISION =
Default decimal precision used by approximate comparisons.
6
Class Method Summary collapse
-
.precision ⇒ Integer
Returns the current thread's default numeric precision.
-
.precision=(precision) ⇒ Integer?
Sets or clears the current thread's default numeric precision.
Instance Method Summary collapse
-
#acos(value) ⇒ Float
Returns the inverse cosine in degrees.
-
#acot(value) ⇒ Float
Returns the inverse cotangent in degrees.
-
#approx(float, precision = nil) ⇒ Numeric
Rounds a float with an explicit or thread-local precision.
-
#asin(value) ⇒ Float
Returns the inverse sine in degrees.
-
#atan(value) ⇒ Float
Returns the inverse tangent in degrees.
-
#atan2(y, x) ⇒ Float
Returns the quadrant-aware inverse tangent in degrees.
-
#cos(degrees) ⇒ Float
Returns the cosine of an angle expressed in degrees.
-
#cot(degrees) ⇒ Float
Returns the cotangent of an angle expressed in degrees.
-
#count(length, division) ⇒ Integer
Counts complete divisions in a length.
-
#eq?(left, right, precision: nil) ⇒ Boolean
Compares two numeric values after approximate rounding.
-
#ge?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is greater than or equal to the rounded right operand.
-
#gt?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is greater than the rounded right operand.
-
#le?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is less than or equal to the rounded right operand.
-
#lt?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is less than the rounded right operand.
-
#round(float, precision) ⇒ Numeric
Rounds a value only when precision is present.
-
#sin(degrees) ⇒ Float
Returns the sine of an angle expressed in degrees.
-
#tan(degrees) ⇒ Float
Returns the tangent of an angle expressed in degrees.
-
#to_degrees(radians) ⇒ Float
Converts radians to degrees.
-
#to_radians(degrees) ⇒ Float
Converts degrees to radians.
-
#with_precision(precision) { ... } ⇒ Object
Runs a block with a current-thread precision override.
-
#zero?(value, precision: nil) ⇒ Boolean
Checks whether a value is approximately zero.
Class Method Details
.precision ⇒ Integer
Returns the current thread's default numeric precision.
16 17 18 19 20 |
# File 'lib/sevgi/function/math.rb', line 16 def self.precision precision = Thread.current.thread_variable_get(PRECISION_KEY) precision.nil? ? PRECISION : precision end |
.precision=(precision) ⇒ Integer?
Sets or clears the current thread's default numeric precision.
25 26 27 |
# File 'lib/sevgi/function/math.rb', line 25 def self.precision=(precision) Thread.current.thread_variable_set(PRECISION_KEY, precision) end |
Instance Method Details
#acos(value) ⇒ Float
Returns the inverse cosine in degrees.
33 |
# File 'lib/sevgi/function/math.rb', line 33 def acos(value) = to_degrees(::Math.acos(value)) |
#acot(value) ⇒ Float
Returns the inverse cotangent in degrees.
38 |
# File 'lib/sevgi/function/math.rb', line 38 def acot(value) = 90.0 - to_degrees(::Math.atan(value)) |
#approx(float, precision = nil) ⇒ Numeric
Rounds a float with an explicit or thread-local precision.
45 46 47 |
# File 'lib/sevgi/function/math.rb', line 45 def approx(float, precision = nil) float.round(precision.nil? ? Function::Math.precision : precision) end |
#asin(value) ⇒ Float
Returns the inverse sine in degrees.
53 |
# File 'lib/sevgi/function/math.rb', line 53 def asin(value) = to_degrees(::Math.asin(value)) |
#atan(value) ⇒ Float
Returns the inverse tangent in degrees.
58 |
# File 'lib/sevgi/function/math.rb', line 58 def atan(value) = to_degrees(::Math.atan(value)) |
#atan2(y, x) ⇒ Float
Returns the quadrant-aware inverse tangent in degrees.
64 |
# File 'lib/sevgi/function/math.rb', line 64 def atan2(y, x) = to_degrees(::Math.atan2(y, x)) |
#cos(degrees) ⇒ Float
Returns the cosine of an angle expressed in degrees.
69 |
# File 'lib/sevgi/function/math.rb', line 69 def cos(degrees) = ::Math.cos(to_radians(degrees)) |
#cot(degrees) ⇒ Float
Returns the cotangent of an angle expressed in degrees.
74 |
# File 'lib/sevgi/function/math.rb', line 74 def cot(degrees) = 1.0 / ::Math.tan(to_radians(degrees)) |
#count(length, division) ⇒ Integer
Counts complete divisions in a length.
81 |
# File 'lib/sevgi/function/math.rb', line 81 def count(length, division) = (length / division.to_f).to_i |
#eq?(left, right, precision: nil) ⇒ Boolean
Compares two numeric values after approximate rounding.
88 |
# File 'lib/sevgi/function/math.rb', line 88 def eq?(left, right, precision: nil) = approx(left, precision) == approx(right, precision) |
#ge?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is greater than or equal to the rounded right operand.
95 |
# File 'lib/sevgi/function/math.rb', line 95 def ge?(left, right, precision: nil) = approx(left, precision) >= approx(right, precision) |
#gt?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is greater than the rounded right operand.
102 |
# File 'lib/sevgi/function/math.rb', line 102 def gt?(left, right, precision: nil) = approx(left, precision) > approx(right, precision) |
#le?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is less than or equal to the rounded right operand.
109 |
# File 'lib/sevgi/function/math.rb', line 109 def le?(left, right, precision: nil) = approx(left, precision) <= approx(right, precision) |
#lt?(left, right, precision: nil) ⇒ Boolean
Checks whether the rounded left operand is less than the rounded right operand.
116 |
# File 'lib/sevgi/function/math.rb', line 116 def lt?(left, right, precision: nil) = approx(left, precision) < approx(right, precision) |
#round(float, precision) ⇒ Numeric
Rounds a value only when precision is present.
122 |
# File 'lib/sevgi/function/math.rb', line 122 def round(float, precision) = precision ? float.round(precision) : float |
#sin(degrees) ⇒ Float
Returns the sine of an angle expressed in degrees.
127 |
# File 'lib/sevgi/function/math.rb', line 127 def sin(degrees) = ::Math.sin(to_radians(degrees)) |
#tan(degrees) ⇒ Float
Returns the tangent of an angle expressed in degrees.
132 |
# File 'lib/sevgi/function/math.rb', line 132 def tan(degrees) = ::Math.tan(to_radians(degrees)) |
#to_degrees(radians) ⇒ Float
Converts radians to degrees.
137 |
# File 'lib/sevgi/function/math.rb', line 137 def to_degrees(radians) = radians.to_f * 180 / ::Math::PI |
#to_radians(degrees) ⇒ Float
Converts degrees to radians.
142 |
# File 'lib/sevgi/function/math.rb', line 142 def to_radians(degrees) = degrees.to_f / 180 * ::Math::PI |
#with_precision(precision) { ... } ⇒ Object
Runs a block with a current-thread precision override.
150 151 152 153 154 155 156 157 158 |
# File 'lib/sevgi/function/math.rb', line 150 def with_precision(precision, &block) ArgumentError.("Block required") unless block previous = Thread.current.thread_variable_get(PRECISION_KEY) Function::Math.precision = precision block.call ensure Function::Math.precision = previous if block end |
#zero?(value, precision: nil) ⇒ Boolean
Checks whether a value is approximately zero.
164 |
# File 'lib/sevgi/function/math.rb', line 164 def zero?(value, precision: nil) = eq?(value, 0.0, precision:) |