Added cos, sin, tan, and atan methods and tests (#96)

* Added Atan method

* added sin and cos methods

* added tests for atan, sin, and cos

* tan method and test

* potentinal fix for './decimal_test.go:261:6: d declared but not used' error thrown by the tip version Travis CI tests + corrected comment in decimal.go
This commit is contained in:
Ian Norden 2018-07-09 09:22:25 -05:00 committed by Victor Quinn
parent 19e3cb6c29
commit 7e43aed1c9
2 changed files with 385 additions and 2 deletions

View file

@ -1144,3 +1144,224 @@ func (d NullDecimal) MarshalJSON() ([]byte, error) {
} }
return d.Decimal.MarshalJSON() return d.Decimal.MarshalJSON()
} }
// Trig functions
// Atan returns the arctangent, in radians, of x.
func (x Decimal) Atan() Decimal {
if x.Equal(NewFromFloat(0.0)) {
return x
}
if x.GreaterThan(NewFromFloat(0.0)) {
return x.satan()
}
return x.Neg().satan().Neg()
}
func (d Decimal) xatan() Decimal {
P0 := NewFromFloat(-8.750608600031904122785e-01)
P1 := NewFromFloat(-1.615753718733365076637e+01)
P2 := NewFromFloat(-7.500855792314704667340e+01)
P3 := NewFromFloat(-1.228866684490136173410e+02)
P4 := NewFromFloat(-6.485021904942025371773e+01)
Q0 := NewFromFloat(2.485846490142306297962e+01)
Q1 := NewFromFloat(1.650270098316988542046e+02)
Q2 := NewFromFloat(4.328810604912902668951e+02)
Q3 := NewFromFloat(4.853903996359136964868e+02)
Q4 := NewFromFloat(1.945506571482613964425e+02)
z := d.Mul(d)
b1 := P0.Mul(z).Add(P1).Mul(z).Add(P2).Mul(z).Add(P3).Mul(z).Add(P4).Mul(z)
b2 := z.Add(Q0).Mul(z).Add(Q1).Mul(z).Add(Q2).Mul(z).Add(Q3).Mul(z).Add(Q4)
z = b1.Div(b2)
z = d.Mul(z).Add(d)
return z
}
// satan reduces its argument (known to be positive)
// to the range [0, 0.66] and calls xatan.
func (d Decimal) satan() Decimal {
Morebits := NewFromFloat(6.123233995736765886130e-17) // pi/2 = PIO2 + Morebits
Tan3pio8 := NewFromFloat(2.41421356237309504880) // tan(3*pi/8)
pi := NewFromFloat(3.14159265358979323846264338327950288419716939937510582097494459)
if d.LessThanOrEqual(NewFromFloat(0.66)) {
return d.xatan()
}
if d.GreaterThan(Tan3pio8) {
return pi.Div(NewFromFloat(2.0)).Sub(NewFromFloat(1.0).Div(d).xatan()).Add(Morebits)
}
return pi.Div(NewFromFloat(4.0)).Add((d.Sub(NewFromFloat(1.0)).Div(d.Add(NewFromFloat(1.0)))).xatan()).Add(NewFromFloat(0.5).Mul(Morebits))
}
// sin coefficients
var _sin = [...]Decimal{
NewFromFloat(1.58962301576546568060E-10), // 0x3de5d8fd1fd19ccd
NewFromFloat(-2.50507477628578072866E-8), // 0xbe5ae5e5a9291f5d
NewFromFloat(2.75573136213857245213E-6), // 0x3ec71de3567d48a1
NewFromFloat(-1.98412698295895385996E-4), // 0xbf2a01a019bfdf03
NewFromFloat(8.33333333332211858878E-3), // 0x3f8111111110f7d0
NewFromFloat(-1.66666666666666307295E-1), // 0xbfc5555555555548
}
// Sin returns the sine of the radian argument x.
func (d Decimal) Sin() Decimal {
PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts
PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000,
PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170,
M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi
if d.Equal(NewFromFloat(0.0)) {
return d
}
// make argument positive but save the sign
sign := false
if d.LessThan(NewFromFloat(0.0)) {
d = d.Neg()
sign = true
}
j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle
y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float
// map zeros to origin
if j&1 == 1 {
j++
y = y.Add(NewFromFloat(1.0))
}
j &= 7 // octant modulo 2Pi radians (360 degrees)
// reflect in x axis
if j > 3 {
sign = !sign
j -= 4
}
z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
zz := z.Mul(z)
if j == 1 || j == 2 {
w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5]))
y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w)
} else {
y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5])))
}
if sign {
y = y.Neg()
}
return y
}
// cos coefficients
var _cos = [...]Decimal{
NewFromFloat(-1.13585365213876817300E-11), // 0xbda8fa49a0861a9b
NewFromFloat(2.08757008419747316778E-9), // 0x3e21ee9d7b4e3f05
NewFromFloat(-2.75573141792967388112E-7), // 0xbe927e4f7eac4bc6
NewFromFloat(2.48015872888517045348E-5), // 0x3efa01a019c844f5
NewFromFloat(-1.38888888888730564116E-3), // 0xbf56c16c16c14f91
NewFromFloat(4.16666666666665929218E-2), // 0x3fa555555555554b
}
// Cos returns the cosine of the radian argument x.
func (d Decimal) Cos() Decimal {
PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts
PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000,
PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170,
M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi
// make argument positive
sign := false
if d.LessThan(NewFromFloat(0.0)) {
d = d.Neg()
}
j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle
y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float
// map zeros to origin
if j&1 == 1 {
j++
y = y.Add(NewFromFloat(1.0))
}
j &= 7 // octant modulo 2Pi radians (360 degrees)
// reflect in x axis
if j > 3 {
sign = !sign
j -= 4
}
if j > 1 {
sign = !sign
}
z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
zz := z.Mul(z)
if j == 1 || j == 2 {
y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5])))
} else {
w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5]))
y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w)
}
if sign {
y = y.Neg()
}
return y
}
var _tanP = [...]Decimal{
NewFromFloat(-1.30936939181383777646E+4), // 0xc0c992d8d24f3f38
NewFromFloat(1.15351664838587416140E+6), // 0x413199eca5fc9ddd
NewFromFloat(-1.79565251976484877988E+7), // 0xc1711fead3299176
}
var _tanQ = [...]Decimal{
NewFromFloat(1.00000000000000000000E+0),
NewFromFloat(1.36812963470692954678E+4), //0x40cab8a5eeb36572
NewFromFloat(-1.32089234440210967447E+6), //0xc13427bc582abc96
NewFromFloat(2.50083801823357915839E+7), //0x4177d98fc2ead8ef
NewFromFloat(-5.38695755929454629881E+7), //0xc189afe03cbe5a31
}
// Tan returns the tangent of the radian argument x.
func (d Decimal) Tan() Decimal {
PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts
PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000,
PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170,
M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi
if d.Equal(NewFromFloat(0.0)) {
return d
}
// make argument positive but save the sign
sign := false
if d.LessThan(NewFromFloat(0.0)) {
d = d.Neg()
sign = true
}
j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle
y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float
// map zeros to origin
if j&1 == 1 {
j++
y = y.Add(NewFromFloat(1.0))
}
z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic
zz := z.Mul(z)
if zz.GreaterThan(NewFromFloat(1e-14)) {
w := zz.Mul(_tanP[0].Mul(zz).Add(_tanP[1]).Mul(zz).Add(_tanP[2]))
x := zz.Add(_tanQ[1]).Mul(zz).Add(_tanQ[2]).Mul(zz).Add(_tanQ[3]).Mul(zz).Add(_tanQ[4])
y = z.Add(z.Mul(w.Div(x)))
} else {
y = z
}
if j&2 == 2 {
y = NewFromFloat(-1.0).Div(y)
}
if sign {
y = y.Neg()
}
return y
}

View file

@ -261,13 +261,13 @@ func TestRequireFromStringErrs(t *testing.T) {
var d Decimal var d Decimal
var err interface{} var err interface{}
func() { func(d Decimal) {
defer func() { defer func() {
err = recover() err = recover()
}() }()
d = RequireFromString(s) d = RequireFromString(s)
}() }(d)
if err == nil { if err == nil {
t.Errorf("panic expected when parsing %s", s) t.Errorf("panic expected when parsing %s", s)
@ -2284,3 +2284,165 @@ func TestRoundBankAnomaly(t *testing.T) {
t.Errorf("Expected bank rounding %s to equal %s, but it was %s", b, expected, bRounded) t.Errorf("Expected bank rounding %s to equal %s, but it was %s", b, expected, bRounded)
} }
} }
// Trig tests
// For Atan
func TestAtan(t *testing.T) {
inps := []string{
"-2.91919191919191919",
"-1.0",
"-0.25",
"0.0",
"0.33",
"1.0",
"5.0",
"10",
"11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
}
sols := []string{
"-1.2407643882205801102743706275310603586882014729198302312846062338790031898128063403419218957424163818359375",
"-0.78539816339744833061616997868383017934410073645991511564230311693950159490640317017096094787120819091796875",
"-0.24497866312686415",
"0.0",
"0.318747560420644443",
"0.78539816339744833061616997868383017934410073645991511564230311693950159490640317017096094787120819091796875",
"1.3734007669450159012323399573676603586882014729198302312846062338790031898128063403419218957424163818359375",
"1.4711276743037346312323399573676603586882014729198302312846062338790031898128063403419218957424163818359375",
"1.5707962358859730612325945023537403586882014729198302312846062338790031898128063403419218957424163818359375",
}
for i, inp := range inps {
d, err := NewFromString(inp)
if err != nil {
t.FailNow()
}
s, err := NewFromString(sols[i])
if err != nil {
t.FailNow()
}
a := d.Atan()
if !a.Equal(s) {
t.Errorf("expected %s, got %s", s, a)
}
}
}
// For Sin
func TestSin(t *testing.T) {
inps := []string{
"-2.91919191919191919",
"-1.0",
"-0.25",
"0.0",
"0.33",
"1.0",
"5.0",
"10",
"11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
}
sols := []string{
"-0.220571862520030093613839729898389234591448264443828979962999436149729548845614563657783710150808249493659603883785557750857064742068654401611592366453156979425089038732558977915147138610177101332138819745624841999009731533470874398791630404814623756725761484209848526893621903868344844362592350061614721709283168025028307789990557501123216276655612701350632840149744527732872833304632776220881820168763715880072271900752263175824768749676997835634284780882959485313122780499486873741058283218325040301421620652217699025347852371634749059985101913743575566482455108757945784327770283152556778940395109157571785095552777237471737930299497127855782791647416409676751196562400230573256919649166116220548146722732673811940013822174052471575484241086300786209457059067702404625074214637302180559257156598478224357461349695824437855046828206155188482292930722180491096361781653423287753045189099820938975700063357767823639345982521057442719437079960883170296730778877529726323641962581167118741985973085143794261349309399843621404104514700512532703761239968546841161640494797236796414402075947283426422877298624337798757146020442229118476349906575010431352541089067121029579490866064323880357132858662072455977109093817967157613567800944577216024589669093782841129397681562208313003621389103425798339230823330581188201904296875",
"-0.84147098480789650683573384227719871264659725213827912307678945963233888758666252541652307542471941569920950873234694687123276222626964952320957348347806244448304658355378064371162688704108432893734339281119107889886243608606771440333499496399246182550579552307436056577989755048676139388257574972355638331020916986327112812449023235994235220681962126402627149122741398037411569225585167209440887381294958909446263040889436515610243685011381400045762457675558838333095494482138227725013381292722259213324814381642325470123171162316406069142833674558352099403808484142312206036567122374357983536120130367436871049536150555321227634134943262101222587962853023526365048081446005272233936756658798560188088496199440605960018665932771208203273875932636591008594752836095901878899636892331191188133444513639520743904232369072391326448163751628578886099960312207261234582748222511007033947148867295201489673608530271710726980420265749989442268185776660104680951516862727780322815011940555693819786827407000470928166813302681223319136916385379500478197064927760223020342764479386095349145229773585152310192696649682836151684354735282894320955021997802761069713870309184654536840910246184967655772931790056052366745119986847633246318705770507591911530119455091063686301656648189606417807689546576464738456252196397075290117772324937902655849801056962584258183367906235526162380299193832940968379238027598232552151102936477400362491607666015625",
"-0.247403959254522929719728461264852108650160857317149198152655832001777826134603088803487480618059635162353515625",
"0.0",
"0.324043028394868345912501559420824735488967340443112210114701957327566252120565337691004970110952854156494140625",
"0.84147098480789650683573384227719871264659725213827912307678945963233888758666252541652307542471941569920950873234694687123276222626964952320957348347806244448304658355378064371162688704108432893734339281119107889886243608606771440333499496399246182550579552307436056577989755048676139388257574972355638331020916986327112812449023235994235220681962126402627149122741398037411569225585167209440887381294958909446263040889436515610243685011381400045762457675558838333095494482138227725013381292722259213324814381642325470123171162316406069142833674558352099403808484142312206036567122374357983536120130367436871049536150555321227634134943262101222587962853023526365048081446005272233936756658798560188088496199440605960018665932771208203273875932636591008594752836095901878899636892331191188133444513639520743904232369072391326448163751628578886099960312207261234582748222511007033947148867295201489673608530271710726980420265749989442268185776660104680951516862727780322815011940555693819786827407000470928166813302681223319136916385379500478197064927760223020342764479386095349145229773585152310192696649682836151684354735282894320955021997802761069713870309184654536840910246184967655772931790056052366745119986847633246318705770507591911530119455091063686301656648189606417807689546576464738456252196397075290117772324937902655849801056962584258183367906235526162380299193832940968379238027598232552151102936477400362491607666015625",
"-0.95892427466313846886683534084169138601699178595056142681129038893400410281163045903621198874295374290149806823897075576057724999482959874362888984630786877750800707948164478439888413437709227541296453939066231042449153560817956529124771991719434464325257307490209688817348630841990228088502580219431623697760216390920129365244393334429239217501291865772417470981461975427863913628459487634361457463896191397206119089801538318057594728767278995563353925328463456560840888895608182514323808823801838724482524462741712473496938668730262585034295941891365544300178534659369917661430555201335114597830077556398746790054730682231578093617522006106659977088453875512527401574137870617360741477467052899927465250688663724908651769559466919777914497651416655110015287957761809374118954830175592272643086681859204507636705728518901196924686915642366269243409467335313632803457424138328976320449572075303129501941586734619627126950347146441488301272523732890971103193432131967093584975094064352688636321088866275878367912478080039614015674090467373532503838989934725811696669260106530065305130564789502177378344025691707116401561519233199329283661803416619394138904068356512359009119169667571681552281997620458915816124425995894145459910451225779660911316891499301549117918834162954131295015995478902576153796243561887919093281708146016725119431774666779045011119428333353937254827947830587875729623490751729608660980375134386122226715087890625",
"-0.544021110889369814404798546234261521735237410341301440744913863554325413910729487222404754912397132389280869582562649416317020824255899391102716253549602359933406667420857908751554064982667505778980864397665237869392769251295144996601379178780771501957858736340861984904014191524939793604688756271913886434778180316203812548425041682948461830667971245994025528380063656691687013885731759172341309655658172703937945366976128597657101230629806658897303549468649147494313422814604383947907342285887634159176609268080528703630505118954169027929142559898709956536517735787676487117591429732597764129997800544321069045676840705169697409709450991200259966568075515292000715246707423119024660809599616729869855242609248565503156233847151639381625642548896051440565640966333400046266460293523862957282305141303657282623793830073310394349418290939283661501995774676808852008306303431767521000117474347792119119745767325061425517820206720145795639782171641765977980488850129569363740173287329545019826050789666603219553652178298235868990551128361160456166647394642535572127727629420370720856130359966967599668830582910424823037763854828905673063487188976311335956440851988180468345725600705105113853947651574770698730911647817190648000646872041955859376392859382055300138695355787127336060458870914402496055117808282375335693359375",
"-0.564291758073920672156060501108780948637502927903402695682970758082173347334144085493099932047342878232107294208163423635351694839181128599190761981992082190183414367145761958978263205360050276797203465120166463337285861568628620441149869080262018002460551105733370384496691552603807079787064355499979892738941692390909198368605275928720995456124329350110140031040327865030504338978174244479702663590952255309812151905391076362043165507118546398673253957125454990703254702293833362635280840010399956637688760201442320428708562854314419339362472820724298986435162931641340060501848890585327411159080467227885852591584159465243368331681937734746160681439404005233844642476786199607795910618003577395193096223575912506207477927572401934790710176468073033112942732454695154492720511230782735184156027155137618173659003878927697237751139877779082916148874086491265428313018347132700490228131397266369640661232003084568637049642401980172435548737268283623715380443755530399410106957744899992161330205161097513785489406292271887390739430612193060474304401657511461250218562733033893187306639264886516827894130028111468708805823753758224864450015655567869605537758979198887215555526950741196023433812962018677695204317878927956155283357420412564940844662156159296973147878187706628631717213628871121500196750275790691375732421875",
}
for i, inp := range inps {
d, err := NewFromString(inp)
if err != nil {
t.FailNow()
}
s, err := NewFromString(sols[i])
if err != nil {
t.FailNow()
}
a := d.Sin()
if !a.Equal(s) {
t.Errorf("expected %s, got %s", s, a)
}
}
}
// For Cos
func TestCos(t *testing.T) {
inps := []string{
"-2.91919191919191919",
"-1.0",
"-0.25",
"0.0",
"0.33",
"1.0",
"5.0",
"10",
"11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
}
sols := []string{
"-0.975370726167463436728505866323714237868918798735337852490354630275400590397256757982930518671177656988788392411709345759154371178337418085499048191038650091945778580197329516971729513331012900776507504230803826799226710606177535289143559059224362661001603838745169803182674368143452935332062804519861768577138064573259069358975042122485627388192604101700302462362798592115038066626006203292151971117547825788365029212778673068841445412348366566041687247190622871297208478844658065023427261675103854131453079888921085982507674462174796122088474426882259116347806427809304457623578281770915649130727943036401841073052876646614496797452163682420483896962238944883515469778180754205404941121141599952645904787654598990827384683885441411829970587806867632776521257761008506280475694989732289455150722563701599552835784792787892378475275568451704113581492707873602587027241507884412830033365653914549639245334821262039117311046735253960816076120011217151854994903031629514573221302055487026138480621515317251868170670711382669181677841187033304861609062981031260474272177938456322715827189878292157663041430578246790062347759217920400856872733420989333068628730367636465064551304273677450773043171019346261214827671284261815921602597267030573778706635242318228554033918329328451836597811660816887574069149657366695399451977163530476815743551656054860762997757628606444388219917589048259254358441694421344436705112457275390625",
"0.5403023058681397185594704958555785318789737039530258806063132922287686731767103380722879951346341700301934154242672285869862285136280035256635149087722026456653060623287636554810379311566025776640284286043366692017812351065106848975433243878434601646053373656175870219143335780037780263780528022552589948882631781788387243759097393407418459341665025578959743680402679565710214589639277870162089735459876789876164945626397889278949094061300698882180972876375063478762441763141045691438279791675986386217937407677843979434037325100615614204173839676230023544297002103390623811623287987178412049715000660987495684907814359780765658960715423795658408220614069561530422742718922893554271137549412369147629593458239490366605797325139239944180051692566520667214675204069476110320173267646327440417945329562672924843423546460029298423629611979148079742086487910832189641973781754022003095306885302971446512196936121388134045074972570593581133444925052909080976768554628154520121141808489601642421723177702083881285314379847744230155051966361158007017591582046619808944751148550841102122653076944135728372456492657514171872121435155305860386442665726120257630353953444221670261220705805623292334551636518107895571396829211893470388375917284217734121333575035514871632238034279250742569584498765995392820238318876135963364504277706146240234375",
"0.968912421710644784098953478920868172610959295777507211448260716808741793725813506998889579335809685289859771728515625",
"1.0",
"0.946042343528386971548655274719127421974169853736868957009626534392384054569270779300182994120405055582523345947265625",
"0.5403023058681397185594704958555785318789737039530258806063132922287686731767103380722879951346341700301934154242672285869862285136280035256635149087722026456653060623287636554810379311566025776640284286043366692017812351065106848975433243878434601646053373656175870219143335780037780263780528022552589948882631781788387243759097393407418459341665025578959743680402679565710214589639277870162089735459876789876164945626397889278949094061300698882180972876375063478762441763141045691438279791675986386217937407677843979434037325100615614204173839676230023544297002103390623811623287987178412049715000660987495684907814359780765658960715423795658408220614069561530422742718922893554271137549412369147629593458239490366605797325139239944180051692566520667214675204069476110320173267646327440417945329562672924843423546460029298423629611979148079742086487910832189641973781754022003095306885302971446512196936121388134045074972570593581133444925052909080976768554628154520121141808489601642421723177702083881285314379847744230155051966361158007017591582046619808944751148550841102122653076944135728372456492657514171872121435155305860386442665726120257630353953444221670261220705805623292334551636518107895571396829211893470388375917284217734121333575035514871632238034279250742569584498765995392820238318876135963364504277706146240234375",
"0.2836621854632262640413454705014792126730041372592792257206146645426601396441260356011571632089582618925838153657699769828738164020634023542401271546832973668591965970780399287846132178786932147719489387057249755694634919271032742386467263343003762216059912285589934893404186137482489686474984451054771103493648603655340770275593072267521598532667125806036270513157359920795997662877180316554125323347714021810156977000099765637673158040510070215919734518464013962899531358902754977185164550708058199871247160015886870149163066556093196783440812874006182781633602366172183573725257608628866745555998550377960547686470245458626674802626292796549871502254193653242073557087898962773658255810053531370802115864997325060870572958692356612208396119690852149106422002310275167937817158288794799498840438933404405058238604063789542363766559068475105058515056790385539505205746157593694133334912162302627008416603294000615739181111749007688625782080015509782711717307370897514073199106746929535880890780214210719294387601483817437596151502942142722181977054762243644235893248214844557965863434535805464134779033144520828071538407409414463996182938827134980848798774952211899634360773590821173224015934994691863733763099257382589344052735164638723090648652823314675360473802118354232333173901927332659182734442282480813446454703807830810546875",
"-0.839071529076452452453116298055375283024545196907005751226679269494945423257632785713674884440909521085910953206821682885350514489569383484553680293874464258013074629595233233440423524040185080138431677219644537006650333440982266180848432613265727672098505530500860099490052714852637696219860623419861733967504942346257426883650461547676693756422205686582533132927538864456011428540560137047577109540901355332714454892042154044099772961016405398585134639556422987015399944634792029504830682794892974110049771680445087591216461375304720930330852114906188605879368500242393092090599991056282929140339515922705640197683216586300345608814191348583643897583052496585038234458432300602598656172228800591628810870703061601079842675931288590090649649722933122710180797353905585641576282107791663819561479911160784057959370842865399560455463184561649904326467913053219927760200406450092385176263660350059186320075841952511620387666555291853410264149054381143798602019026379808898830628940629734269442061546806433026794983473131041596411391519359668360706593453379582689919628495055879290310156211842712529358591580000339115277557733264569390191184923563095676385834461753760385376605508361399123052214852866448517012467160704995276728328831739643710159503245565807479821789258404871375420699263398077037976545179716664243275062643380243567701961405078734621807138136709079831010972563517559541512724763379083015024662017822265625",
"-0.825575442809343611824362325239811764094510857054278582264067433862269037693599066167126550050055810893236466513641579446123546053932400340779013543022627343780304207889697484852104716170210387967098979368968667582761587933736919053415297219989844530337025837550322738303417999408570541023209479831053490074672524731593393200211612747267787376420912055466715589243670137116181283653627378091767001266729750523670904813955028404725364549436654773973161631448021217524149606880882398338428340541423336476442278764927060929088442662022809429880029452211569992113848964950861453209500601571720208371076364637312918805739974730928391130012733244321591317884640789514132100142038718855057870855384682083191451172141637084656463668180074273357513986650562276924927490092054490207521493248546634803683905992004777812863248355102626794030220223795127136641375381651724046389855784239897808572076152799212203148992979849462537797025824681362178656342679605636982892165755312758468261428421302359039383473990693745647491195856344807310687765672840471336376933282820265780965117564858639226791165667469629053398278240657443123596015059287606479740468263567953594727359402416208210264162078048896806628308236533843407536450642386891796397818074083632855343175466156403711148552219179067451933509186842662133841059218230787130271284099320325244795528905606899665072032213644123281103167139222988313296269780039438046514987945556640625",
}
for i, inp := range inps {
d, err := NewFromString(inp)
if err != nil {
t.FailNow()
}
s, err := NewFromString(sols[i])
if err != nil {
t.FailNow()
}
a := d.Cos()
if !a.Equal(s) {
t.Errorf("expected %s, got %s", s, a)
}
}
}
// For Tan
func TestTan(t *testing.T) {
inps := []string{
"-2.91919191919191919",
"-1.0",
"-0.25",
"0.0",
"0.33",
"1.0",
"5.0",
"10",
"11000020.2407442310156021090304691671842603586882014729198302312846062338790031898128063403419218957424",
}
sols := []string{
"0.226141565050579195128504091376587961550743024795950198353192839504353407742431869564825319685041904449462890625",
"-1.5574077246549022",
"-0.255341921221036275",
"0.0",
"0.342524867530038963",
"1.5574077246549022",
"-3.3805150062465857",
"0.648360827459086657992079596346674836000458368506527382600293171822426231687419573290753760375082492828369140625",
"0.683513254892486971935899897806108130234891903543492515192842067427622421368508870073710248504975849182264766875",
}
for i, inp := range inps {
d, err := NewFromString(inp)
if err != nil {
t.FailNow()
}
s, err := NewFromString(sols[i])
if err != nil {
t.FailNow()
}
a := d.Tan()
if !a.Equal(s) {
t.Errorf("expected %s, got %s", s, a)
}
}
}