rpl.rb/lib/core/trig.rb

103 lines
2.9 KiB
Ruby
Raw Normal View History

2021-12-02 15:33:22 +01:00
module Rpl
2022-01-18 17:07:25 +01:00
module Lang
module Core
module_function
2021-12-02 15:33:22 +01:00
2022-01-18 17:07:25 +01:00
# pi constant
def pi( stack, dictionary )
stack << { type: :numeric,
base: 10,
value: BigMath.PI( Rpl::Lang::Core.precision ) }
2022-01-18 17:07:25 +01:00
[stack, dictionary]
end
# sinus
def sinus( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
end
2022-01-18 22:30:16 +01:00
# https://rosettacode.org/wiki/Trigonometric_functions#Ruby
2022-01-18 17:07:25 +01:00
# arg sinus
def arg_sinus( stack, dictionary )
# # Handle angles with no tangent.
# return -PI / 2 if y == -1
# return PI / 2 if y == 1
# # Tangent of angle is y / x, where x^2 + y^2 = 1.
# atan(y / sqrt(1 - y * y, prec), prec)
2022-01-18 22:30:16 +01:00
stack << { value: '
«
2022-01-18 17:07:25 +01:00
dup abs 1 ==
« pi 2 / * »
« dup sq 1 swap - sqrt / atan »
ifte
»',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# cosinus
def cosinus( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
end
# arg cosinus
def arg_cosinus( stack, dictionary )
stack << { value: '« dup 0 == « pi 2 / » « dup sq 1 swap - sqrt / atan dup 0 < « pi + » ift » ifte »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# tangent
def tangent( stack, dictionary )
stack << { value: '« dup sin swap cos / »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# arg tangent
def arg_tangent( stack, dictionary )
stack, args = Rpl::Lang::Core.stack_extract( stack, [%i[numeric]] )
stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], Rpl::Lang::Core.precision ), Rpl::Lang::Core.precision ) }
[stack, dictionary]
end
# convert degrees to radians
def degrees_to_radians( stack, dictionary )
stack << { value: '« 𝛑 * 180 / »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
# convert radians to degrees
def radians_to_degrees( stack, dictionary )
stack << { value: '« 180 * 𝛑 / »',
type: :program }
Rpl::Lang::Core.eval( stack, dictionary )
end
2021-12-02 15:33:22 +01:00
end
end
end