rpl.rb/lib/rpl/words/trig.rb

85 lines
3.4 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
module Words
module Trig
2022-02-26 18:53:39 +01:00
include Types
def populate_dictionary
super
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['𝛑', 'pi'],
'Trig on reals and complexes',
'( … -- 𝛑 ) push 𝛑',
proc do
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplNumeric, BigMath.PI( RplNumeric.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
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric]] )
2022-01-18 17:07:25 +01:00
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplNumeric, BigMath.sin( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.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',
2022-02-28 11:40:47 +01:00
Types.new_object( RplProgram, '« 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-26 18:53:39 +01:00
ifte »' ) )
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
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric]] )
2022-01-18 17:07:25 +01:00
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplNumeric, BigMath.cos( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
end )
@dictionary.add_word( ['acos'],
'Trig on reals and complexes',
'( n -- m ) compute arg-cosinus of n',
2022-02-28 11:40:47 +01:00
Types.new_object( RplProgram, '« dup 0 ==
« drop 𝛑 2 / »
2022-01-20 10:55:13 +01:00
«
dup sq 1 swap - sqrt / atan
dup 0 <
« 𝛑 + »
ift
»
2022-02-26 18:53:39 +01:00
ifte »' ) )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['tan'],
'Trig on reals and complexes',
'( n -- m ) compute tangent of n',
2022-02-28 11:40:47 +01:00
Types.new_object( RplProgram, '« dup sin swap cos / »' ) )
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
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric]] )
2022-02-28 11:40:47 +01:00
@stack << Types.new_object( RplNumeric, BigMath.atan( BigDecimal( args[0].value, RplNumeric.precision ), RplNumeric.precision ) )
2022-02-26 18:53:39 +01:00
end )
2022-01-18 17:07:25 +01:00
@dictionary.add_word( ['d→r', 'd->r'],
'Trig on reals and complexes',
'( n -- m ) convert degree to radian',
2022-02-28 11:40:47 +01:00
Types.new_object( RplProgram, '« 180 / 𝛑 * »' ) )
2022-02-26 18:53:39 +01:00
@dictionary.add_word( ['r→d', 'r->d'],
'Trig on reals and complexes',
'( n -- m ) convert radian to degree',
2022-02-28 11:40:47 +01:00
Types.new_object( RplProgram, '« 𝛑 180 / / »' ) )
end
2021-12-02 15:33:22 +01:00
end
end
end