irritating: bc float expression leads to integer result - bc

I have been happily using bc for a few weeks. Now I need to do some simple calculations like 1.0+27.0/37.0*5.0, which bc in interactive mode calculates as integer result. I tried this on two different linux boxes. Same result: 1 instead of 4.67...
Can you suggest an explanation?

Are you really using bc for several weeks? By default bc computes on integer numbers. You may change it either by typing first:
scale=8
(or whatever number of digits you need).
Or by launching bc with: bc -l which is often the most convenient way since it also loads some scientific functions.

Related

How do I specify the printing precision for Sympy.pretty_print?

I've been trying to figure this out from the Sympy docs, but I can't find any options or parameters that control precision for printed output. I don't want to change the precision of the stored numbers, I just don't need 17 digits of precision in my output. Any good way to do this?
import sympy as sp
sp.init_printing() # presumably options would go here?
a = sp.pi
sp.pprint(sp.N(a)) # I'd like to see only a few digits here, not full precision

python floating point, why is the print result different (examples given)?

a = (random.random(), random.random())
print(a)
print(a[0])
the result is:
(0.4817527913069962, 0.7017598562799067)
0.481752791307
What extra is happening behind printing a tuple(similar behavior for list)? Why is there extra fraction?
Thanks a lot.
BTW, this is python 2.7
What you are seeing is the difference between the formatting choices made by str(float) and repr(float). In Python 2.x, str(float) returns 12 digits while repr(float) returns 17 digits. In its interactive mode, Python uses str() to format the result. That accounts for the 12 digits of precision when formatting a float. But when the result is a tuple or list, the string formatting logic uses repr() to format each element.
The output of repr(float) must be able to be converted back to the original value. Using 17 digits of precision guarantees that behavior. Python 3 uses a more sophisticated algorithm that returns the shortest string that will round-trip back to the original value. Since repr(float) frequently returns a more friendly appearing result, str(float) was changed to be the same as repr(float).

Numeric Range Regular Expression to 1 decimal place

I'm trying to write regular expressions for the following numeric ranges however need some assistance to write them correctly.
All numeric values to match against are to 1 decimal place.
0.0 To 59.9
60.0 to 119.9
120.0 to 239.9
240.0 to 419.9
1200.0 to 1799.9
Anything greater than 3600.0
I believe I've manage to correctly get the correct Regular expression for the 0.0 to 59.9 range however am having difficulty with the others.
^[1-5]?([1-9](?:\.[0-9])?|0?\.[1-9])$
check this:
// 0.0 to 59.9
^[012345]?[0-9]\.[0-9]$
//60.0 to 119.9
^(?:[6789]|10|11)[0-9]\.[0-9]$
//120.0 to 239.9
^(?:1[0-9]{2}|2[0123][0-9])\.[0-9]$
//240.0 to 419.9
^(?:2[456789][0-9]|3[0-9]{2}|4[01][0-9])\.[0-9]$
//1200.0 to 1799.9
^(?:1[234567])[0-9]{2}\.[0-9]$
//Anything greater than 3600.0
^(?:36[0-9]{2}|3[789][0-9]{2}|[456789][0-9]{3}|[0-9]{5}[0-9]*)\.[0-9]$
You're using the wrong tool for the job. While you could use a regex to do what you're trying to do, it's not well suited to do it well let alone efficiently. A simple if, else if, and else construct will suit you the best. If you want to ignore anything after the first decimal place when doing the check, you can use .toFixed(1) and then convert it back to a float using parseFloat(string). You could also use Math.round(float) depending on how you want to treat numbers with more than one decimal place.
Note: The specific functions given are JavaScript examples, depending on your language they may be slightly different.

How to correctly add floating numbers in Python?

I am trying to add 0.2 value to constant x where x = 8 in a loop that runs to 100. Following is the code
x = 8
>>> for i in range(100):
... x += 0.2
...
>>> x
but everytime I get different answer and calculation always incorrect. I read about Floating Point Arithmetic Issue and Limitations but there should be some way around this. Can I use doubles (if they exists) ? I am using Python 2.7
UPDATE:
import time
x=1386919679
while(1):
x+=0.02
print "xx %0.9f"%x
b= round (x,2)
print "bb %0.9f"%b
time.sleep(1)
output
xx 1386933518.586801529
bb 1386933518.589999914
xx 1386933518.606801510
bb 1386933518.609999895
xx 1386933518.626801491
bb 1386933518.630000114
Desired output
I want correct output, I know If just write print x it will be accurate. But my application require that I should print results with 9 precision. I am newbie so please be kind.
You can use double-precision floating point, sure. You're already using it by default.
As for a way around it:
x += 0.2 * 100
I know that sounds facile, but the solution to floating point imprecision is not setting FLOATING_POINT_IMPRECISION = False. This is a fundamental limitation of the representation, and has no general solution, only specific ones (and patterns which apply to groups of specific situations).
There's also a rational number type which can exactly store 0.2, but it's not worth considering for most real-world use cases.

compare two files containing doubles

I want to check equality of two files containing double numbers.
I should consider near numbers equal e.g. differences of at most 0.0001.
It's easy to write a tester for that with C but is there an easier way? e.g. bash commands?
Here is one way you can do it:
paste file1 file2 | awk '{d=$1-$2;if((d<0?-1*d:d)>0.0001) print $0 " " d }'
First use paste to print out corresponding lines. Then pass them to awk to subtract. Calculate the absolute difference and check if it is more than your tolerance of 0.00001. If so, print out both values and the difference.
Bash does not provide operators for floating point manipulations. You can look up bc and it should be fairly easy to integrate it in a bash script.
See this article on Linux Journal. That should provide you with a starting point. It is upto you to work through the file structure.