I want a floating point precision like Fortran in python program. In Fortran using pi gives a value of 3.141592653589793238462643 while using same numpy.pi gives a value of 3.141592653589793. Is it possible to get floating point accuracy like Fortran in python? Tried float128 but did not work. Any suggestion will be highly appreciated.
You can use mpmath to create floats of an arbitrary precision:
from mpmath import mp
mp.dps = 25
mypi = (mp.quad(lambda x: mp.exp(-x**2), [-mp.inf, mp.inf]) ** 2)
print mypi # 3.141592653589793238462643
Related
I have a JsonArray with an entry like: [1502054710762,-93.787]
I am using the QDecimal class in my project to work with real decimal values instead of float or double.
Now my problem is, that the value in the JsonArray is not a string. It is a JsonNumber as decimal. If I try to read the value throuh array.at(1).toDouble(), I generate an inaccurate decimal value.
Interesting point: this only happens on my Linux server, but runs fine on my Notebook. - No idea why...
You can't use toDouble() to get it out as that changes the precision available. You're going to have to do your math with QDecimal.
How do you increase the floating point precision representation of tensorflow variables? I need this cause my network is large and data is complex, so I want to see any amount of improvement no matter how small. When I iterate through the training I occasionally print the mean error to the screen, and all I see is the same 6 digits - it works fine with less complex inputs. Note that tensorboard seems to have similar precision, I would be happy enough with a more precise tensorboard graph.
msquaredError=m_sqerror.eval(sessions=sess,feed_dict={input:ip, output=op,keep_prob:1.0})
print ("MSE: %9f"%msquaredError)
output:
MSE: 0.317513
desired output:
MSE: 0.317513223 ... and many more digits
Stoopid error.
print ("MSE: %9f"%msquaredError)
should be
print ("MSE: %.9f"%msquaredError)
In python 2.7, i am adding below numbers in the given order(since i am getting, those data from db in the same order)
24.73+1+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+8
summation is coming as 39.99999999999997
but actual answer is 40
any suggestion will be helpful. thanks in advance
From the Python documentation on floats:
Note that this is in the very nature of binary floating-point: this is
not a bug in Python, and it is not a bug in your code either. You’ll
see the same kind of thing in all languages that support your
hardware’s floating-point arithmetic (although some languages may not
display the difference by default, or in all output modes).
So, this is just the way floating numbers work. To get the results you are expecting, try using the decimal module.
>>> 24.73+1+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+8
39.99999999999997
>>> s = "24.73+1+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+.33+8"
>>> sum([Decimal(x) for x in s.split('+')])
Decimal('40.00')
I am trying to round up a number using math module in python.
So when I do,
print math.ceil(21/10)
I get '2.0' which is right.
print math.ceil(27/10)
I still get '2.0'
I want to get 3, since it is closest to 2.7
Could someone please advise a workaround.
Thanks in advance.
You are being surprised by the division operator in Python 2.x. With integers, it does integer division; 21/10 results in 2 and 27/10 results in 2.
Use 21.0/10 and 27.0/10 and you will get the correct answers.
In Python 3.x, division of integers will automatically promote to float if the division isn't even (there would be a remainder). You can get this behavior in Python 2.7 by using from __future__ import division.
By the way, pretty sure the integer ceiling of 21/10 should be 3.
I think you want round:
from __future__ import division
print round(27/10)
3.0
print round(21/10)
2.0
math.ceil will always round up, round will round to the nearest
You only get 2 from math.ceil(21/10) because of how python2 handles integer division.
21/10 in python2 is 2
The problem is Python thinks 27/10 are integers and so the evaluates that as 2. If you write 27/10.0 it will make them floats and the thing will work as you want.
I have one number which I need to find the ceiling and the floor value of (203,400) in order to use this number to create a weighted average. From this number I want: 200,000 and 210,000 so the code I was using that doesn't work is:
S1CovA_ceil = ceil(S1CovA,10000);
S1CovA_floor = floor(S1CovA,10000);
When I run this program, I get these errors:
ERROR 72-185: The CEIL function call has too many arguments.
ERROR 72-185: The FLOOR function call has too many arguments.
Does anybody know a way around this or different SAS code I could use?
CEIL and FLOOR only remove decimals - specifically rounding to integer value. If you want it rounded to (above/below) multiple of 10,000, you have to do it a bit more complicatedly:
S1CovA_ceil = ceil(s1covA/10000)*10000;
And the same for floor. Basically you have to divide it by the desired rounding level, round the rest with ceil/floor, and then multiply back.
Unfortunately, as far as I'm aware, SAS doesn't allow rounding in a particular direction except for straight integer rounding.
You can also use the round() function...
%LET ROUNDTO = 10000 ;
data xyz ;
S1CovA_ceil = round(S1CovA+(&ROUNDTO / 2),&ROUNDTO) ;
S1CovA_floor = round(S1CovA-(&ROUNDTO / 2),&ROUNDTO) ;
run ;
Try
S1CovA_ceil = ceil(S1CovA/10000)*10000;
S1CovA_floor = floor(S1CovA/10000)*10000;