Mar-02-2019, 12:03 PM
Hy there, im new to python, just getting the gist of it. I want to build me a program, and i need decimal precision, therefore i learned that i need to use the decimal type instead of float, because i need to calculate numbers with 100 decimal precision.
I noticed here
https://docs.python.org/3/library/decimal.html
there are built in functions, for Pi, Sin, Cos
copy paste them in the text editor/py charm, and testing them works as advertised, if i set 100 or 150 decimal precision.
However, for my program, i also need arccos function, which can be built easily from the arcsin function. There isnt any in the docs file, so i decided to built it myself, using the taylor expansion series for arcsin. This can be seen here
![[Image: unknown.png]](https://cdn.discordapp.com/attachments/481779566159855617/551368892967288832/unknown.png)
these are the codes for sin and cos in decimal
Cosinus
and Sinus
this is the function i made myself for arcsinus
The problem is i cant use the Arcisnus function that i made, unless the variable given to it is of float type, whereas the first two functions somehow work with decimals. This is puzzling to me, as i cannot seen any difference in the the functions what so ever
There are 2 more functions i use
this is the function for PI, also taken from decimal tutorial docs
and another function to convert from degrees to radians
Here it is how it goes
I have an angle of 78 degreees
Passing this through the cp_rads function gives me as a result
1.361356816555577070000478799421117916485440073062545855755809323333387109390690566072148424314917396
This is of decimal type
Passing this through the cp_sin function gives me
0.9781476007338056379285667478695995324597378088626771078851776636405968331200951219997585254547856367
So the Sin function works with decimals.
However if i have as a test number for the arcsin function
Test = 0.876
running it through the cp_arcsin function returns me
1.0675052793425073
Checking its type, python tells me its float.
If i assign the test variable
passing this through the cp_arcsin function will yield me an error.
So how come the functions that i copied from the decimal tutorial recipes paragraph work with decimal type numbers, while the function i made, only works with float numbers ?
I need an arcsin function that can yield me 100 digit precision results, and i would need to make it use decimal type variables, yet it defies all logic and doesnt seem to work with decimal types.
Since i just started learning python for 2 days, perhaps someone can enlightne me, maybe i'm missing somehting ?
Do note that my code starts with
I noticed here
https://docs.python.org/3/library/decimal.html
there are built in functions, for Pi, Sin, Cos
copy paste them in the text editor/py charm, and testing them works as advertised, if i set 100 or 150 decimal precision.
However, for my program, i also need arccos function, which can be built easily from the arcsin function. There isnt any in the docs file, so i decided to built it myself, using the taylor expansion series for arcsin. This can be seen here
![[Image: unknown.png]](https://cdn.discordapp.com/attachments/481779566159855617/551368892967288832/unknown.png)
these are the codes for sin and cos in decimal
Cosinus
1 2 3 4 5 6 7 8 9 10 11 12 |
def cp_cos(x): getcontext().prec + = 2 i, lasts, s, fact, num, sign = 0 , 0 , 1 , 1 , 1 , 1 while s ! = lasts: lasts = s i + = 2 fact * = i * (i - 1 ) num * = x * x sign * = - 1 s + = num / fact * sign getcontext().prec - = 2 return + s |
1 2 3 4 5 6 7 8 9 10 11 12 |
def cp_sin(x): getcontext().prec + = 2 i, lasts, s, fact, num, sign = 1 , 0 , x, 1 , x, 1 while s ! = lasts: lasts = s i + = 2 fact * = i * (i - 1 ) num * = x * x sign * = - 1 s + = num / fact * sign getcontext().prec - = 2 return + s |
1 2 3 4 5 6 7 8 9 10 11 12 |
def cp_arcsin(x): getcontext().prec + = 2 i, lasts, s, m, e, n = 1 , 0 , x, 1 , 0 , 0 while s ! = lasts: lasts = s m * = ( 2 * i - 1 ) / ( 2 * i) e = 2 * i + 1 n = ((x * * e) / e) s + = m * n i + = 1 getcontext().prec - = 2 return + s |
There are 2 more functions i use
this is the function for PI, also taken from decimal tutorial docs
1 2 3 4 5 6 7 8 9 10 11 12 |
def cp_pi(): getcontext().prec + = 2 # extra digits for intermediate steps three = Decimal( 3 ) # substitute "three=3.0" for regular floats lasts, t, s, n, na, d, da = 0 , three, 3 , 1 , 0 , 0 , 24 while s ! = lasts: lasts = s n, na = n + na, na + 8 d, da = d + da, da + 32 t = (t * n) / d s + = t getcontext().prec - = 2 return + s # unary plus applies the new precision |
1 2 3 4 5 6 7 |
def cp_rads(x): getcontext().prec + = 2 rads, grads, mult = 0 , x, 0 mult = cp_pi() / 180 rads = grads * mult getcontext().prec - = 2 return + rads |
I have an angle of 78 degreees
Passing this through the cp_rads function gives me as a result
1.361356816555577070000478799421117916485440073062545855755809323333387109390690566072148424314917396
This is of decimal type
Passing this through the cp_sin function gives me
0.9781476007338056379285667478695995324597378088626771078851776636405968331200951219997585254547856367
So the Sin function works with decimals.
However if i have as a test number for the arcsin function
Test = 0.876
running it through the cp_arcsin function returns me
1.0675052793425073
Checking its type, python tells me its float.
If i assign the test variable
1 |
Test = Decimal( 0.876 ) |
So how come the functions that i copied from the decimal tutorial recipes paragraph work with decimal type numbers, while the function i made, only works with float numbers ?
I need an arcsin function that can yield me 100 digit precision results, and i would need to make it use decimal type variables, yet it defies all logic and doesnt seem to work with decimal types.
Since i just started learning python for 2 days, perhaps someone can enlightne me, maybe i'm missing somehting ?
Do note that my code starts with
1 2 3 |
from decimal import * MyCustomContext = Context (prec = 100 , rounding = None ) setcontext(MyCustomContext) |