I want to use "math.sqrt" and my output should have 4 decimal after point even for numbers like "4". is there any func or way?!
I used "round(num_sqrt, 4)" but it didn't work.
my input is like:
1
2
3
19
output must be:
1.0000
1.4142
1.7320
4.3588
and my output is:
1.0
1.4142
1.7320
4.3588
Try this
from decimal import Decimal
import math
# example with sqrt function
y = Decimal(math.sqrt(4))
z = round(y, 4)
print(z) # output 2.0000
# First we take a float and convert it to a decimal
x = Decimal(16.0/7)
print(x)
# Then we round it to 4 places
output = round(x,4)
print(output) # outpun 2.2857
Try this if you really need unnecessary zeros:
def format_floats(reference, values):
formatted_values = []
for i in range(len(values)):
length = len(str(reference)[str(reference).find("."):])-1
new_float = str(round(values[i], length))
new_float += "0"*(len(str(reference))-len(new_float))
formatted_values.append(new_float)
return formatted_values
if __name__ == '__main__':
reference = 0.12345
values = [1.04, 2.045, 2.0]
print(format_floats(reference, values))
output: ['1.04000', '2.04500', '2.00000']
EDIT: solved but since the solution was in the comments and I cant accept my own solution reffering to the comment till tomorrow it is still open. Once again a big thank you to this great community and its people
optional context: I am computing sollutions for the Pell equation
http://mathworld.wolfram.com/PellEquation.html
On the buttom of the page is a table with values for D -> x, y.
My code works perfectly for EVERY VALUE EXCEPT D = 61. I believe it could have something to do with the values of x and y being very big and maybe the fraction module cant handle such big numbers and there is an overflow?
I made the observation, that whether I give my input/ starting value as a fraction or a decimal changes my solution (but only for D = 61).
Why is my code failing with the value of D = 61? What do I need to change/use to get it to work? Thank you very much for your time and help.
code:
from math import sqrt, floor
from fractions import Fraction
def continued_fraction(D):
# to make sure it is not a problem on converting decimals to fractions I made EVERYTHING a fraction (which shouldnt and didnt affect the output)
# input is the value for D, output is a tuple with (x, y)
D = Fraction(sqrt(D))
aS = []
a0 = D
r1 = Fraction(D - floor(D))
a = Fraction(a0 - r1)
r = Fraction(-1)
count = 0
while a <= 2*floor(D):
aS.append((a, count))
if a == 2*floor(D):
if count % 2 == 0:
break
else:
r = count
if count == 2*r:
break
try:
a0 = Fraction(1/r1)
except ZeroDivisionError:
break
r1 = Fraction(a0 - floor(a0))
a = Fraction(a0 - r1)
count += 1
pS = []
qS = []
a0 = Fraction(floor(D))
p0 = a0
p1 = Fraction(a0 * aS[1][0] + 1)
q0 = Fraction(1)
q1 = Fraction(aS[1][0])
count = 2
while count < len(aS):
pS.append((p0, count - 2))
qS.append((q0, count - 2))
pn = Fraction(aS[count][0] * p1 + p0)
qn = Fraction(aS[count][0] * q1 + q0)
p0 = Fraction(p1)
p1 = Fraction(pn)
q0 = Fraction(q1)
q1 = Fraction(qn)
count += 1
pS.append((p0, count-1))
#pS.append((p1, count))
qS.append((q0, count - 1))
#qS.append((q1, count))
#print(pS)
#print(qS)
return Fraction(pS[-1][0]), Fraction(qS[-1][0])
print(continued_fraction(Fraction(61)))
Fraction(1/r1) means to compute the reciprocal of r1 as an inexact floating-point number, and then find a rational approximation of that inexact number. You want Fraction(1, r1) to directly specify the numerator and denominator of your fraction, without any approximation errors creeping in.
A big thanks to GalAbra and jasonharper for your responds. After knowing with certainty, that it is a percision problem (thank you GalAbra) I knew I needed more decimals for the sqrt(D). I used the decimal module from Python:
from decimal import *
getcontext().prec = 1000
D = Fraction(Decimal(D).sqrt())
with this and the change suggested by jasonharper (thank you again) it works now.
I want to find all bases and exponents of a number.
Example:
Number = 64
2^6=64
4^3=64
8^2=64
64^1=64
Number = 1845.28125
4.5^5=1845.28125
Number = 19683
3^9=19683
27^3=19683
19683^1=19683
What I do now is to make an integer of 'Number' and just see of the results of multiple calculations gives the correct result:
basehits, expohits = [], []
if eval(Number) > 1000:
to = 1000 #base max 1000 in order to avoid too many calculations
else:
to = int(eval(Number))
for n in range(1,to):
for s in range(1,31): #just try with exponents from 1 to 30
calcres = pow(n,s)
if calcres == eval(Number):
basehits.append(n)
expohits.append(s)
elif calcres > eval(Number):
break
The problem is that this never find a Floating Number as for example 1845.28125 (see above).
Is there a better way to find exponents and bases when only the result is known?
Your problem needs more constraints, but here's some help:
>>> from math import log
>>> help(log)
Help on built-in function log in module math:
log(...)
log(x[, base])
Return the logarithm of x to the given base.
If the base not specified, returns the natural logarithm (base e) of x.
>>> for base in range(2, 10):
... exp = log(64, base)
... print('%s ^ %s = %s' % (base, exp, base ** exp))
...
2 ^ 6.0 = 64.0
3 ^ 3.785578521428744 = 63.99999999999994
4 ^ 3.0 = 64.0
5 ^ 2.5840593484403582 = 63.99999999999999
6 ^ 2.3211168434072493 = 63.99999999999998
7 ^ 2.1372431226481328 = 63.999999999999964
8 ^ 2.0 = 64.0
9 ^ 1.892789260714372 = 63.99999999999994
how about
import math
num=64
for i in range(2,int(math.sqrt(num))+1):
if math.log(num,i).is_integer():
print i,int(math.log(num,i))
the output is:
2 6
4 3
8 2
and of course, you can always add:
print num,1
to get
64,1
If you want to add fractions, with n decimal digits after the dot, you can use this:
from __future__ import division
import math
num=1845.28125
decimal_digits=1
ans=3
x=1
while(ans>=2):
ans=num**(1/x)
if (ans*10**decimal_digits).is_integer():
print ans,x
x+=1
where decimal_digits indicates the number of places after the dot.
For this example the answer will be
4.5 5,
If you change for example num to 39.0625 and decimal_digits to 2, the output will be:
2.5 4
6.25 2
Integers
For integers you could look at the prime factors of your number. Once you know that 64 is 2**6, it's easy to list all the results you wanted.
Now, which result do you expect for numbers that have at least 2 different prime factors? For example : should 15 be written 3*5, 3**1 * 5**1 or 15**1?
Floats
It's not clear how your problem is defined for Floats.
What's special about 4.5?
If you calculate 1845.28125**(1.0/5), Python returns 4.5, but for other input numbers, the result might be off by 1e-16.
Possible solution
import math
def find_possible_bases(num, min_base = 1.9, max_decimals = 9, max_diff = 1e-15):
max_exponent = int(math.ceil(math.log(num,min_base)))
for exp in range(1,max_exponent):
base = round(num**(1.0/exp),max_decimals)
diff = abs(base**exp-num)
if diff < max_diff:
print('%.10g ** %d = %.10g' % (base, exp, base ** exp))
find_possible_bases(64)
# 64 ** 1 = 64
# 8 ** 2 = 64
# 4 ** 3 = 64
# 2 ** 6 = 64
find_possible_bases(19683)
# 19683 ** 1 = 19683
# 27 ** 3 = 19683
# 3 ** 9 = 19683
find_possible_bases(1845.28125)
# 1845.28 ** 1 = 1845.28
# 4.5 ** 5 = 1845.28
find_possible_bases(15)
# 15 ** 1 = 15
It iterates over possible exponents, and calculates what the base would be. It rounds it to 9 decimals, and checks what the error becomes. If it's small enough, it displays the result. You could play with the parameters and find what best suits your problem.
As a bonus, it also works fine with Integers (e.g. 64 and 15).
It might be better to work with Rational numbers.
For my school project I was trying to compute the value of using different methods. One of the formula I found was the Machin Formula that can be calculated using the Taylor expansion of arctan(x).
I wrote the following code in python:
import decimal
count = pi = a = b = c = d = val1 = val2 = decimal.Decimal(0) #Initializing the variables
decimal.getcontext().prec = 25 #Setting percision
while (decimal.Decimal(count) <= decimal.Decimal(100)):
a = pow(decimal.Decimal(-1), decimal.Decimal(count))
b = ((decimal.Decimal(2) * decimal.Decimal(count)) + decimal.Decimal(1))
c = pow(decimal.Decimal(1/5), decimal.Decimal(b))
d = (decimal.Decimal(a) / decimal.Decimal(b)) * decimal.Decimal(c)
val1 = decimal.Decimal(val1) + decimal.Decimal(d)
count = decimal.Decimal(count) + decimal.Decimal(1)
#The series has been divided into multiple small parts to reduce confusion
count = a = b = c = d = decimal.Decimal(0) #Resetting the variables
while (decimal.Decimal(count) <= decimal.Decimal(10)):
a = pow(decimal.Decimal(-1), decimal.Decimal(count))
b = ((decimal.Decimal(2) * decimal.Decimal(count)) + decimal.Decimal(1))
c = pow(decimal.Decimal(1/239), decimal.Decimal(b))
d = (decimal.Decimal(a) / decimal.Decimal(b)) * decimal.Decimal(c)
val2 = decimal.Decimal(val2) + decimal.Decimal(d)
count = decimal.Decimal(count) + decimal.Decimal(1)
#The series has been divided into multiple small parts to reduce confusion
pi = (decimal.Decimal(16) * decimal.Decimal(val1)) - (decimal.Decimal(4) * decimal.Decimal(val2))
print(pi)
The problem is that I am getting the right value of pi only till 15 decimal places, no matter the number of times the loop repeats itself.
For example:
at 11 repetitions of the first loop
pi = 3.141592653589793408632493
at 100 repetitions of the first loop
pi = 3.141592653589793410703296
I am not increasing the repetitions of the second loop as arctan(1/239) is very small and reaches an extremely small value with a few repetitions and therefore should not affect the value of pi at only 15 decimal places.
EXTRA INFORMATION:
The Machin Formula states that:
π = (16 * Summation of (((-1)^n) / 2n+1) * ((1/5)^(2n+1))) - (4 * Summation of (((-1)^n) / 2n+1) * ((1/239)^(2n+1)))
That many terms is enough to get you over 50 decimal places. The problem is that you are mixing Python floats with Decimals, so your calculations are polluted with the errors in those floats, which are only precise to 53 bits (around 15 decimal digits).
You can fix that by changing
c = pow(decimal.Decimal(1/5), decimal.Decimal(b))
to
c = pow(1 / decimal.Decimal(5), decimal.Decimal(b))
or
c = pow(decimal.Decimal(5), decimal.Decimal(-b))
Obviously, a similar change needs to be made to
c = pow(decimal.Decimal(1/239), decimal.Decimal(b))
You could make your code a lot more readable. For starters, you should put the stuff that calculates the arctan series into a function, rather than duplicating it for arctan(1/5) and arctan(1/239).
Also, you don't need to use Decimal for everything. You can just use simple Python integers for things like count and a. Eg, your calculation for a can be written as
a = (-1) ** count
or you could just set a to 1 outside the loop and negate it each time through the loop.
Here's a more compact version of your code.
import decimal
decimal.getcontext().prec = 60 #Setting precision
def arccot(n, terms):
base = 1 / decimal.Decimal(n)
result = 0
sign = 1
for b in range(1, 2*terms, 2):
result += sign * (base ** b) / b
sign = -sign
return result
pi = 16 * arccot(5, 50) - 4 * arccot(239, 11)
print(pi)
output
3.14159265358979323846264338327950288419716939937510582094048
The last 4 digits are rubbish, but the rest are fine.
How do I get the numbers after a decimal point?
For example, if I have 5.55, how do i get .55?
5.55 % 1
Keep in mind this won't help you with floating point rounding problems. I.e., you may get:
0.550000000001
Or otherwise a little off the 0.55 you are expecting.
Use modf:
>>> import math
>>> frac, whole = math.modf(2.5)
>>> frac
0.5
>>> whole
2.0
What about:
a = 1.3927278749291
b = a - int(a)
b
>> 0.39272787492910011
Or, using numpy:
import numpy
a = 1.3927278749291
b = a - numpy.fix(a)
Using the decimal module from the standard library, you can retain the original precision and avoid floating point rounding issues:
>>> from decimal import Decimal
>>> Decimal('4.20') % 1
Decimal('0.20')
As kindall notes in the comments, you'll have to convert native floats to strings first.
An easy approach for you:
number_dec = str(number-int(number))[1:]
Try Modulo:
5.55%1 = 0.54999999999999982
To make it work with both positive and negative numbers:
try abs(x)%1. For negative numbers, without with abs, it will go wrong.
5.55 % 1
output 0.5499999999999998
-5.55 % 1
output 0.4500000000000002
import math
orig = 5.55
whole = math.floor(orig) # whole = 5.0
frac = orig - whole # frac = 0.55
similar to the accepted answer, even easier approach using strings would be
def number_after_decimal(number1):
number = str(number1)
if 'e-' in number: # scientific notation
number_dec = format(float(number), '.%df'%(len(number.split(".")[1].split("e-")[0])+int(number.split('e-')[1])))
elif "." in number: # quick check if it is decimal
number_dec = number.split(".")[1]
return number_dec
>>> n=5.55
>>> if "." in str(n):
... print "."+str(n).split(".")[-1]
...
.55
Just using simple operator division '/' and floor division '//' you can easily get the fraction part of any given float.
number = 5.55
result = (number/1) - (number//1)
print(result)
Sometimes trailing zeros matter
In [4]: def split_float(x):
...: '''split float into parts before and after the decimal'''
...: before, after = str(x).split('.')
...: return int(before), (int(after)*10 if len(after)==1 else int(after))
...:
...:
In [5]: split_float(105.10)
Out[5]: (105, 10)
In [6]: split_float(105.01)
Out[6]: (105, 1)
In [7]: split_float(105.12)
Out[7]: (105, 12)
Another example using modf
from math import modf
number = 1.0124584
# [0] decimal, [1] integer
result = modf(number)
print(result[0])
# output = 0124584
print(result[1])
# output = 1
This is a solution I tried:
num = 45.7234
(whole, frac) = (int(num), int(str(num)[(len(str(int(num)))+1):]))
Float numbers are not stored in decimal (base10) format. Have a read through the python documentation on this to satisfy yourself why. Therefore, to get a base10 representation from a float is not advisable.
Now there are tools which allow storage of numeric data in decimal format. Below is an example using the Decimal library.
from decimal import *
x = Decimal('0.341343214124443151466')
str(x)[-2:] == '66' # True
y = 0.341343214124443151466
str(y)[-2:] == '66' # False
Use floor and subtract the result from the original number:
>> import math #gives you floor.
>> t = 5.55 #Give a variable 5.55
>> x = math.floor(t) #floor returns t rounded down to 5..
>> z = t - x #z = 5.55 - 5 = 0.55
Example:
import math
x = 5.55
print((math.floor(x*100)%100))
This is will give you two numbers after the decimal point, 55 from that example. If you need one number you reduce by 10 the above calculations or increase depending on how many numbers you want after the decimal.
import math
x = 1245342664.6
print( (math.floor(x*1000)%1000) //100 )
It definitely worked
Another option would be to use the re module with re.findall or re.search:
import re
def get_decimcal(n: float) -> float:
return float(re.search(r'\.\d+', str(n)).group(0))
def get_decimcal_2(n: float) -> float:
return float(re.findall(r'\.\d+', str(n))[0])
def get_int(n: float) -> int:
return int(n)
print(get_decimcal(5.55))
print(get_decimcal_2(5.55))
print(get_int(5.55))
Output
0.55
0.55
5
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.
Source
How to get rid of additional floating numbers in python subtraction?
You can use this:
number = 5.55
int(str(number).split('.')[1])
This is only if you want toget the first decimal
print(int(float(input()) * 10) % 10)
Or you can try this
num = float(input())
b = num - int(num)
c = b * 10
print(int(c))
Using math module
speed of this has to be tested
from math import floor
def get_decimal(number):
'''returns number - floor of number'''
return number-floor(number)
Example:
n = 765.126357123
get_decimal(n)
0.12635712300004798
def fractional_part(numerator, denominator):
# Operate with numerator and denominator to
# keep just the fractional part of the quotient
if denominator == 0:
return 0
else:
return (numerator/ denominator)-(numerator // denominator)
print(fractional_part(5, 5)) # Should be 0
print(fractional_part(5, 4)) # Should be 0.25
print(fractional_part(5, 3)) # Should be 0.66...
print(fractional_part(5, 2)) # Should be 0.5
print(fractional_part(5, 0)) # Should be 0
print(fractional_part(0, 5)) # Should be 0
Easier if the input is a string, we can use split()
decimal = input("Input decimal number: ") #123.456
# split 123.456 by dot = ['123', '456']
after_coma = decimal.split('.')[1]
# because only index 1 is taken then '456'
print(after_coma) # '456'
if you want to make a number type
print(int(after_coma)) # 456
a = 12.587
b = float('0.' + str(a).split('.')[-1])
What about:
a = 1.234
b = a - int(a)
length = len(str(a))
round(b, length-2)
Output:
print(b)
0.23399999999999999
round(b, length-2)
0.234
Since the round is sent to a the length of the string of decimals ('0.234'), we can just minus 2 to not count the '0.', and figure out the desired number of decimal points. This should work most times, unless you have lots of decimal places and the rounding error when calculating b interferes with the second parameter of round.
You may want to try this:
your_num = 5.55
n = len(str(int(your_num)))
float('0' + str(your_num)[n:])
It will return 0.55.
number=5.55
decimal=(number-int(number))
decimal_1=round(decimal,2)
print(decimal)
print(decimal_1)
output: 0.55
See what I often do to obtain numbers after the decimal point in python
3:
a=1.22
dec=str(a).split('.')
dec= int(dec[1])
If you are using pandas:
df['decimals'] = df['original_number'].mod(1)