How to write exponential representation - python

I would like to know how to write the equivalent representation to the following number in python:
-3.3999999521443642e+38
i did the following:
print(-3.3999999521443642*math.exp(38))
is it correct?

Python supports the scientific notation just as you stated in your question with e in the number:
>>> a=-3.3999999521443642e+38
>>> print(a)
-3.3999999521443642e+38
>>> type(a)
<class 'float'>

you can use as is
a = -3.3999999521443642e+38
print(a)
output
-3.3999999521443642e+38

Python understands this representation directly.
x = 44e10
print (x)
440000000000.0
It is just an elegant way to represent big numbers in a compact way.

Related

String of bits to binary format Python

This seems really simple but I can't figure it out.
I have a string of bits in a string format and want to convert it to a binary format.
I assumed placing the string inside of the bin() function would work but it doesn't.
string = "01101"
print(bin(string))
string = "01101"
print(bin(int(string,2)))
It depends what you mean by binary format.
Here's a few examples of what you can do:
>>> int('01101', 2)
13
>>> number = 13
>>> bin(number)
'0b1101'
>>> oct(number)
'0o15'
>>> hex(number)
'0xd'
>>> f'{number:08b}'
'00001101'

Comma as decimal point in python

I am new to python.
In python, I wish to convert float variables to string variable with 2 decimal places and decimal comma.
For example, 3.1415 --> 3,14
it works fine. But when I convert 1.20, it gives 1,2 instead of 1,20.
I want the latter one
Is there an easy way to achieve that? Thank You Guys
My code is in the following:
s=float(input())
a=round(s,2)
x=str(a)
y=x.replace('.',','))
print(y)
Try using this:
>>> num = 1.201020
>>> '{:.2f}'.format(num).replace('.', ',')
'1,20'

What does a dot after an integer mean in python?

I am looking at this line of python code (which seems to run properly):
import numpy as np
yl = 300 + 63*np.exp(-x/35.)
What is the dot doing after the 35? what does it do? Is it a signal to python that 35 is a float and not an integer? I have not seen this before. Thanks!
This is easy to test, and you're right. The dot signals a float.
$ python
>>> 1.
1.0
>>> type(1.)
<type 'float'>
Float
Next time, try to explore this using Python
r= 34.
print type(r)
Output: <type 'float'>
It tells python to treat 3 as a float(). Its just a convenient way to make a number a float for division purposes then having to explicitly call float() on it.
For example:
my_float = 3.
typed_float = float(3)
my_float == typed_float
#=> True
type(my_float)
#=> <type 'float'>
In this case you need to typecast to a float to avoid the pitfalls of integer division.

How to properly use string formatting in Python 2.7?

Ok, please bear with me, I'm completely new to this. This is for school, and the exact syntax isn't important here for my assignment, but for my own personal knowledge I want to know a little more about string formatting.
whenever I use %f it defaults to 2 decimal places. Is there a string format that I can use on a float that will show the float with the number of decimals it actually has?
for instance my list contains 2.0, 2.01, 2.001, 2.0001 and I want to use a string format to print them as they look. Which format code would I use or how could I use %f properly if possible?
This is in Python 2.7 on Windows 7(if that matters).
%g may be what you're looking for:
>>> "%g, %g, %g, %g" % (2.1, 2.01, 2.001, 2.0001)
'2.1, 2.01, 2.001, 2.0001'
If you convert the float to a string, then when you print it, it will be displayed just as you wrote it.
>>> x = str(2.0)
>>> print(x)
2.0
>>> x = str(2.01)
>>> print(x)
2.01
>>> x = str(2.001)
>>> print(x)
2.001
>>> x = str(2.0001)
>>> print(x)
2.0001
(In fact, to be precise, in Python you're not actually converting the floating point object, but creating a string object that looks like it. But that's a bit outside of the scope of your question.)
UPDATE
Someone posted a way to remove trailing zeros from floating point numbers using the Decimal class, here: Removing Trailing Zeros in Python

Best way to detect floating round off error in python

So I have a list of floating numbers, some of them have round off errors and appears in the form 0.3599999. It is trivial to detect by convert it to string and see if there is a bunch of 999 following. I wonder how a python hacker will do for this or if there is a mathematical way to do this.
Thanks
Consider using Python's decimal module
>>> from decimal import Decimal
>>> Decimal(0.35)
Decimal('0.34999999999999997779553950749686919152736663818359375')
Also have a look at Numpy's assert_approx_equal() function:
>>> np.testing.assert_approx_equal(0.12345677777777e-20, 0.1234567e-20)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345671e-20,
significant=8)
>>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345672e-20,
significant=8)
...
<type 'exceptions.AssertionError'>:
Items are not equal to 8 significant digits:
ACTUAL: 1.234567e-021
DESIRED: 1.2345672000000001e-021

Categories