Python modulo between logarithm [duplicate] - python

This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 5 years ago.
I'm trying to check in Python if a math.log(x, base) has decimal or not:
import math
# math.log(8, 2) = 3
print math.log(8)
print math.log(2)
print math.log(8) / math.log(2)
print 2.07944154168 % 0.69314718056
print math.log(8) % math.log(2)
Output is:
2.07944154168
0.69314718056
3.0
0.0
0.69314718056
Why does fourth print row return zero but fifth does not?

This will probably be closed for being a duplicate, but just so one can see how this plays out:
>>> import math
>>> math.log(8)
2.0794415416798357
>>> math.log(2)
0.6931471805599453
Now let's say you need to compute math.log(8) % math.log(2). You need to compute the remainder after dividing math.log(2) into math.log(8). Let's see, does it go in 3 times?
0.6931471805599453
+ 0.6931471805599453
+ 0.6931471805599453
--------------------
2.0794415416798359
Woah! We overshot the value of 2.0794415416798357 which means it actually goes in TWO times:
0.6931471805599453
+ 0.6931471805599453
--------------------
1.3862943611198906
Okay so what is the remainder?
2.0794415416798359
- 1.3862943611198906
--------------------
0.6931471805599453
So TL;DR your remainder is close to math.log(2) because of rounding errors. It does not go in exactly three times. It goes in two times with just about math.log(2) left over.
Yes when you print the quotient it says 3.0 but again, this is all rounding error in floating point, which is not unique to Python.

This is what I get using python 3
>>> print (math.log(8))
2.0794415416798357
>>> print (math.log(2))
0.6931471805599453
>>> print (math.log(8) / math.log(2))
3.0
>>> print (2.07944154168 % 0.69314718056)
0.0
>>> print (math.log(8) % math.log(2))
0.6931471805599452
>>> print (2.0794415416798357 % 0.6931471805599453)
0.6931471805599452
It looks like in your example (python 2 ?), the precision of math.log is not enough.

Related

is there a function in python to round off three digits after decimal but show all three digits even if zero [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 3 months ago.
i want to round off number 7.00087 and output should be 7.000
I tried round() function but it eliminates zero.
This function gives the right output:
def my_round(x, decimals):
str_decimals = str(x % int(x))
return str(int(x)) + str_decimals[1 : 2 + decimals]
What does my_round() do:
1- It retrieves decimals in a string format as a variable named str_decimals
2- Concatenates rounded int(x) with desired decimals and return as output.
Let's apply it:
Input
x = 7.00087
y = my_round(x, 3)
print(y)
Ouput
>>> 7.000

Is python % operator broken [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
I've written some script to help me out finding if something is divisible by other thing, and it fails with larger numbers:
print(x, d)
90744169766518547761620274468374441720233236940 10
print(x/d)
9.074416976651854e+45
print(x / (x/d))
10.0
print(x % (x/d))
2.535301200456459e+30
Since 10.0 is clearly lacking decimal part I don't undertand why % is giving me this trash output?
Does this do what you expect?
>>> print(x//d)
9074416976651854776162027446837444172023323694
>>> print(x // (x//d))
10
>>> print(x % (x//d))
0
The difference is that / in Python 3 always produces a floating point result, even if the operands are integers. In this it differs from C. If you want integer division you need to use //.

Is there a way to know whether something was rounded up or down in python? [duplicate]

This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
Closed 2 years ago.
I basically want to know whether the result of my equation (which is a simple one like this x / y) was rounded up or down.
The reason is that I have two simple statements after the rounding line like this:
if h % 2 != 0: h = h + 1
if h % 4 != 0: h = h + 2
and based on the direction of the rounding I would choose the + or - operator, so if the result was rounded up and h % 2 != 0 then it would be h = h + 1 and if it was rounded down then h = h - 1.
Does round() give that kind of information?
Also, is my math correct? (I want the result to be dividable by 4)
Try this to round to 4 directly :
import math
h = 53.75
rounded = math.round(h / 4) * 4
if (rounded > h):
print("Rounded up by " + str(rounded - h))
else:
print("Rounded down by " + str(h - rounded))
Using round() if the number after the given decimal is:
>=5 that + 1 will be added to the final value.
<5 that the final value will return as is to the mentioned decimals.
But you can use ceil or floor from the math package where it always rounds up or down, respectively.
import math
>>> math.ceil(5.2)
6
>>> math.floor(5.9)
5
Let's say you want to know whether 3.9 and 4.4 were rounded. You can do something like this:
def is_rounded_down(val, ndigits=None):
return round(val, ndigits) < val
Then you can simply call the function to find out
>>> is_rounded_down(3.9)
False
>>> is_rounded_down(4.4)
True
By default round() doesn't give that information, so you need to check yourself.
For Python 2.X integer division returns an integer and always rounds down.
add#LM1756:~$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
>>> print 8/3
2
>>> print type(5/2)
<type 'int'>
For Python 3.X integer division returns a float, and so there is no rounding.
add#LM1756:~$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
>>> print(8/3)
2.6666666666666665
>>> type(8/3)
<class 'float'>
>>>

How to print a given number of decimal places? [duplicate]

This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 3 years ago.
I want to print my result with a specific number of decimal places inside of a for loop where the value of it is the number of decimal places to be printed.
Below is a sample of the relevant part of the code:
for i in range (-15, -7):
print ('Valor do erro:' , 10**i, 'Valor da serie:', count, '------->', '%.16f' % adder(count))
Format-specifiers can be nested:
>>> for i in range(1, 5):
... print("{:.{}f}".format(1/i, i))
...
1.0
0.50
0.333
0.2500
Here, 1/i goes to the first (outer) {...} and i to the second (inner) {}.
Note that the number of decimal places can not be negative, though. For this, you might just want to use scientific notation instead.
>>> for i in range(-2, 3):
... print("{:.2e}".format(10**i))
...
1.00e-02
1.00e-01
1.00e+00
1.00e+01
1.00e+02

How do remove trailing numbers in python [duplicate]

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.

Categories