Python Forum
Using Decimal Type instead of Float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Decimal Type instead of Float
#1
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]

these are the codes for sin and cos in decimal

Cosinus
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
and Sinus
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
this is the function i made myself for arcsinus
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
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
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
and another function to convert from degrees to radians
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
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
Test = Decimal(0.876)
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
from decimal import *
MyCustomContext = Context (prec=100, rounding=None)
setcontext(MyCustomContext)
Reply
#2
It seems no python programmers figured this out yet !
Well, i did, and only started using python 2 days ago.

Lets see if the "veterans" figure this out !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Precision with Decimal Type charlesrkiss 9 634 Jan-18-2024, 06:30 PM
Last Post: charlesrkiss
  Write Null values as 0.0 (float) type in csv mg24 3 1,306 Dec-07-2022, 09:04 PM
Last Post: deanhystad
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,023 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  calculate_bai -- TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' pantherd 1 3,195 Apr-21-2020, 12:31 PM
Last Post: anbu23
  TypeError: can't multiply sequence by non-int of type 'float' DimosG 3 3,317 Apr-04-2020, 05:16 AM
Last Post: michael1789
  Type hinting - return type based on parameter micseydel 2 2,425 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 3,324 Jul-25-2019, 11:32 PM
Last Post: scidam
  testing for Decimal w/o importing decimal every time Skaperen 7 4,362 May-06-2019, 10:23 PM
Last Post: Skaperen
  TypeError: unsupported operand type(s) for *: 'datetime.timedelta' and 'float' chris0147 2 37,382 May-01-2018, 07:20 PM
Last Post: nilamo
  Type float doesn't have expected attribute '__div__' sparkz_alot 8 14,708 May-23-2017, 08:22 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020