rpl.rb/lib/rpl/core/trig.rb

101 lines
3.9 KiB
Ruby
Raw Normal View History

2022-02-10 14:50:59 +01:00
# frozen_string_literal: true
2021-12-02 15:33:22 +01:00
# https://rosettacode.org/wiki/Trigonometric_functions#Ruby
module RplLang
2022-02-10 14:50:59 +01:00
module Core
module Trig
def populate_dictionary
super
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['𝛑', 'pi'],
'Trig on reals and complexes',
'( … -- 𝛑 ) push 𝛑',
proc do
@stack << { type: :numeric,
base: 10,
value: BigMath.PI( precision ) }
end )
2022-02-10 14:50:59 +01:00
@dictionary.add_word( ['sin'],
'Trig on reals and complexes',
'( n -- m ) compute sinus of n',
proc do
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.sin( BigDecimal( args[0][:value], precision ), precision ) }
end )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['asin'],
'Trig on reals and complexes',
'( n -- m ) compute arg-sinus of n',
proc do
run( '
2022-01-18 17:07:25 +01:00
dup abs 1 ==
2022-01-20 10:55:13 +01:00
« 𝛑 2 / * »
2022-01-18 17:07:25 +01:00
« dup sq 1 swap - sqrt / atan »
2022-02-08 15:45:36 +01:00
ifte' )
end )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['cos'],
'Trig on reals and complexes',
'( n -- m ) compute cosinus of n',
proc do
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.cos( BigDecimal( args[0][:value], precision ), precision ) }
end )
@dictionary.add_word( ['acos'],
'Trig on reals and complexes',
'( n -- m ) compute arg-cosinus of n',
proc do
run( '
2022-01-20 10:55:13 +01:00
dup 0 ==
« drop 𝛑 2 / »
2022-01-20 10:55:13 +01:00
«
dup sq 1 swap - sqrt / atan
dup 0 <
« 𝛑 + »
ift
»
2022-02-08 15:45:36 +01:00
ifte' )
end )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['tan'],
'Trig on reals and complexes',
'( n -- m ) compute tangent of n',
proc do
run( 'dup sin swap cos /' )
end )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['atan'],
'Trig on reals and complexes',
'( n -- m ) compute arc-tangent of n',
proc do
args = stack_extract( [%i[numeric]] )
2022-01-18 17:07:25 +01:00
@stack << { type: :numeric,
base: infer_resulting_base( args ),
value: BigMath.atan( BigDecimal( args[0][:value], precision ), precision ) }
end)
@dictionary.add_word( ['d→r', 'd->r'],
'Trig on reals and complexes',
'( n -- m ) convert degree to radian',
proc do
run( '180 / 𝛑 *' )
end )
@dictionary.add_word( ['r→d', 'r->d'],
'Trig on reals and complexes',
'( n -- m ) convert radian to degree',
proc do
run( '𝛑 180 / /' )
end)
end
2021-12-02 15:33:22 +01:00
end
end
end