This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Print number in engineering format
How do I print numbers in scientific notation with powers that are multiples of 3? For example:
1.5e4 --> 15e3
1.27e-8 --> 12.7e-9
2.9855e-11 --> 29.855e-9
I'm looking for a function similar to the ENG button on a calculator. Is there a Python package somewhere that does this?
It appears there isn't such a feature yet (at least in Python 2.7), see: http://bugs.python.org/issue8060
On the page http://bytes.com/topic/python/answers/616948-string-formatting-engineering-notation I found the following solution (which I personally don't like that much, but seems to work):
import math
for exponent in xrange(-10, 11):
flt = 1.23 * math.pow(10, exponent)
l = math.log10(flt)
if l < 0:
l = l - 3
p3 = int(l / 3) * 3
multiplier = flt / pow(10, p3)
print '%e =%fe%d' % (flt, multiplier, p3)
Just adapt it according to your needs.
EDIT:
Please look here, too Print number in engineering format
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 days ago.
Improve this question
I am new to coding in general, so after learning the basics of python from various videos on youtube, I started taking challenges on code wars. there is a particular problem I cant seem to get past. here it is :
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k * n.
In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n and p will always be given as strictly positive integers.
I tried using if statements but I still was not able to find the k integer, then I tried using a for loop I still had no progress. I will love it if someone gave an assistance on how to get pass this problem.
This is a problem about math and arithmetic, so it helps to think first about math and arithmetic. You can think about programming once your math is right. Math comes first. Here you are asking whether a ** p + b ** (p+1) + ... is a multiple of n. Or in other words, if it is divisible by n. See Wikipedia: Divisor or some other better math resource about arithmetic and divisibility.
a, b, c, d, ... are known. p is known. So the big number of the left of the equal sign is known. Let's call X this number. X = a ** p + b ** (p+1) + ....
n is known too.
You are asking whether X is a multiple of n. There is division and remainder for that. The answer is yes if and only if the remainder x % n is 0, and in that case, k is the quotient k = X // n.
Important note about python: powers in python are noted using **, not ^. This is important because ^ is also an operator in python, but it doesn't mean power at all. So if you write a ^ p you won't get an error with a helpful error message; instead, the code will execute but the result will be garbage. Be careful to write a ** p and not a ^ p.
This question already has answers here:
How to find integer nth roots?
(11 answers)
Closed 7 years ago.
Recently I encountered a problem:
I want to calculate various roots of various numbers like this:
x = x ** 1/y+1
None of the methods I know result in a working code.
Method 1:
x = 54
y = 2
x = x ** 1/y+1
print(x)
Printed value is 28.0 instead of 3.7798
Method 2:
x = 54
y = 2
x = x ** 1/(y+1)
print(x)
Printed value is 18.0 istead of 3.7798
Method 3:
x = 216
y = 2
x = x ** (1/(y+1))
print(x)
Printed value is 5.99 instead of 6
Is there a way that would work with y being up to 20?
Edit:
Another suggested method:
def nth_root(val, n):
ret = int(val**(1./n))
return ret + 1 if (ret + 1) ** n == val else ret
y = 1
print(nth_root(19, (y+1)))
prints 4
Since everyone else has already told you why your Method 3 is correct, I'll stick to getting you an accurate answer. You can read more about why you're not getting exactly 6, but basically it's because your computer doesn't represent the 1/3 exactly when doing the calculation and makes the final answer off.
So, the easiest solution is to use sympy:
import sympy
y = 216
x = 2
x = sympy.root(y,x+1)
print(x)
You don't seem to understand (yet) order of operations in a programming language. You need parentheses to make sure you add 1 to y, then take the reciprocal, and then use that as an exponent. The "natural" order is the opposite.
x = x ** (1.0/(y+1))
What you want is this (assuming you are using Python 3):
x = x ** (1/(y+1))
For Python 2, either of the following will work:
from __future__ import division
x = x ** (1/(y+1))
or (also fine on Python 3):
x = x ** (1.0/(y+1))
The issue is you need to apply the parentheses in the correct locations to get the order of operations right.
Method 3 is to do with floating point arithmetic. See: https://docs.python.org/3.5/tutorial/floatingpoint.html
For more info on Python 2 vs. Python 3 division:
Division in Python 2.7. and 3.3
Only your last code works because ** has higher precedence than / (and / has higher precendence than +).
The value is not exactly 6, because floating point numbers are not perfectly accurate. A third can not be represented as a float.
All your values are just as expected. According to the python operator precedence:
x ** 1/y+1 is parsed as ((x ** 1) / y) + 1, and
x ** 1/(y+1) is actually (x ** 1) / (y + 1).
What you probably want is x ** (1. / (y + 1)). Note, that 1. is a floating point number, causing the whole expression to be evaluated as floats. This also means that you will work with finite precision, e.g., getting 5.99999 instead fo 6 is to be expected.
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 8 years ago.
I am working on a python project and get a problem.
I have a for loop that makes a list with numbers. The problem is when I print out the list to check if the numbers are correct.
Some of the more simpler calculations are:
2.6 * 3 and 1.3 * 9
If you check with a calculator you should get 7.8 and 11.7, but I get 7.800000000000001 and 11.700000000000001
Which is not a big problem, but I don't like how it looks. (First world problem, I know)
Is there any way to fix it?
Use string format?
print "%.2f" % (2.6 * 3)
The %.2f means print a float to 2dp
You can use format to display the output you want:
>>> print(format(2.6 * 3, ".1f"))
7.8
Here ".1f" means "floating point number to one decimal place".
You can use format print.
res = 2.6*3
"%0.2f" % res
Here's a complete example with a list.
>>> n = [1.9, 7.8 , 9,3.4]
>>> print n
[1.8999999999999999, 7.7999999999999998, 9, 3.3999999999999999]
>>> twodecimals = ["%.2f" % v for v in n]
>>> print twodecimals
['1.90', '7.80', '9.00', '3.40']
>>> print twodecimals[0]
1.90
%.2f means prints only 2 decimal points. Similarly, .1f means 1 decimal point and so on.
This question already has answers here:
floating point equality in Python and in general
(8 answers)
Closed 9 years ago.
I'm learning python and have been trying various things... for some reason this isn't working!
x = (-2.1)
if ( (0.4*(x)) - (0.02*(x)) + (1.396) ) == 0.598:
print "TRUE!"
else:
print "FALSE!"
print ( (0.4*(x)) - (0.02*(x)) + (1.396) )
It prints FALSE! followed by 0.598 obviously the answer is 0.598 so why does'nt the "if" statement work? Thanks!
If you try to print out
(0.38 * x) + 1.396 # note simplified maths and syntax
You will quickly see why:
0.5979999999999999
This is not exactly equal to 0.598, because of the way floating point numbers (float) work. You are better testing these using tolerance:
a = (0.38 * x) + 1.396
if abs(a - 0.598) < 0.0001:
You're confused because python print is using float.__str__, which in turn trims up to 12 digits, so numbers are not what is printed:
>>> ( (0.4*(x)) - (0.02*(x)) + (1.396) ).__str__()
'0.598'
>>> ( (0.4*(x)) - (0.02*(x)) + (1.396) ).__repr__()
'0.5979999999999999'
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
python - decimal place issues with floats
In [4]: 52+121.2
Out[4]: 173.19999999999999
Short answer: Python uses binary arithmetic for floating-point numbers, not decimal arithmetic. Decimal fractions are not exactly representable in binary.
Long answer: What Every Computer Scientist Should Know About Floating-Point Arithmetic
If you're familiar with the idea that the number "thirteen point two" is written in base ten as "13.2" because it's "10^1 * 1 + 10^0 * 3 + 10^-1 * 2" then try to do the same thing with a base of 2 instead of 10 for the number 173.2.
Here's the whole part:
(1 * 2^7) + (0 * 2^6) + (1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (1 * 2^2) + (0 * 2^1) + (0 * 2^0)
Now here's the start fractional part:
(0 * 2^-1) + (0 * 2^-2) + (1 * 2^-3)
That's .125, which isn't yet 2/10ths so we need more additions that are of the form (1 * 2^-n), we can carry this out a bit further with (1 * 2^-4) + (1 * 2^-7), which gets us a bit closer ... to 0.1953125, but no matter how long we do this, we'll never get to ".2" because ".2" is not representable as a addition of sums of numbers of the form (1 * 2^-n).
Also see .9999… = 1.0 (http://en.wikipedia.org/wiki/0.999...)
Try this:
>>> from decimal import Decimal
>>> Decimal("52") + Decimal("121.2")
Decimal("173.2")
The other answers, pointing to good floating-point resources, are where to start. If you understand floating point roundoff errors, however, and just want your numbers to look prettier and not include a dozen extra digits for that extra last bit of precision, try using str() to print the number rather than repr():
>>> 52+121.2
173.19999999999999
>>> str(52+121.2)
'173.2'