I was reading through the documentation to the Python Standard Library. In section 4.4. Numeric Types there is a note to int:
Conversion from floating point to integer may round or truncate as in
C
What does this mean? I thought int always returned the floor? Is that not the case?
>>> print(int(0.4))
0
>>> print(int(0.6))
0
Conversion from a float to a integer truncates towards 0 as it does in C. This is basically equivalent to math.floor(abs(x))*sgn(x), where sgn(x) gives the sign of a number.
Related
I was wondering if theres any easy and fast way to obtain the decimal value of a signed hex in python.
What I do to check the value of a hex code is just open up python in terminal and type in the hex, it returns the decimal like this:
>>>0x0024
36
>>>0xcafe
51966
So I was wondering, is there any way that I could do that for signed short hex? for example 0xfffc should return -4
import ctypes
def hex2dec(v):
return ctypes.c_int16(v).value
print hex2dec(0xfffc)
print hex2dec(0x0024)
print hex2dec(0xcafe)
You can use the int classmethod from_bytes and provide it with the bytes:
>>> int.from_bytes(b'\xfc\xff', signed=True, byteorder='little')
-4
If you can use NumPy numerical types you could use np.int16 type conversion
>>> import numpy as np
>>> np.int16(0x0024)
36
>>> np.int16(0xcafe)
-13570
>>> np.int16(0xfffc)
-4
>>> np.uint16(0xfffc)
65532
>>> np.uint16(0xcafe)
51966
Python Language specifies a unified representation for integer numbers, the numbers.Integral class, a subtype of numbers.Rational. In Python 3 there are no specific constraints on the range of this type. In Python 2 only the minimal range is specified, that is numbers.Integral should have at least the range of -2147483648 through 2147483647, that corresponds to the 32 bit representation.
NumPy implementation is closer to the machine architecture. To implement numeric operations efficiently it provides fixed width datatypes, such as int8, int16, int32, and int64.
I want to convert this hex string '8436d4ccd436d3333' to IEEE floating point. I've try to do this with struct.unpack but it's requires a string argument of length 4.
struct.unpack('>f', binascii.unhexlify('8436d999a436e0000'))
I'm using this website to verify if my conversion attempts are correct : https://gregstoll.dyndns.org/~gregstoll/floattohex/ but I can't find a way to do this.
Thanks for any help
At a guess, each hex string contains two single-precision floating-point values, not one, and the initial 8 is part of the whatever message protocol is being used, and not a part of either of those floats. With that guess, I get some plausible-looking numbers:
>>> struct.unpack('>ff', binascii.unhexlify('436d4ccd436d3333'))
(237.3000030517578, 237.1999969482422)
>>> struct.unpack('>ff', binascii.unhexlify('436d999a436e0000'))
(237.60000610351562, 238.0)
And to reinforce the plausibility, here's what I get by encoding the corresponding 1-digit-past-the-decimal-point values:
>>> binascii.hexlify(struct.pack('>ff', 237.3, 237.2))
b'436d4ccd436d3333'
>>> binascii.hexlify(struct.pack('>ff', 237.6, 238.0))
b'436d999a436e0000'
http://learnpythonthehardway.org/book/ex4.html
There's an extra credit question asking me to explain why the floating-point 4.0 is used instead of 4.
I understand that a floating point is used for accuracy, but I can't fathom why it is necessary in any case in this example.
There doesn't actually seem to be any need for a float instead of an int in that particular example. It could have an effect if you were to divide something by it, but that's not happening here. (And even then, it'd depend on whether you were using Python 2 or 3, as float division is the default in 3).
If you look at the comments below, zedshaw (the author) admits as much:
Михаил Груздев: And why 4.0 used for space? Maybe it's drivers variable value should be floating-point?
zedshaw: Simply to introduce floating point as a little puzzle. Mathematically the exercise doesn't make much sense, it's just practice. Continue on for now.
A floating point is used because in Python an int divided by an int produces an int (integer division) which is not intended here. If you divide a float by an int or an int by a float, you get a float.
Example:
4/3
=> 1
4.0/3
=> 1.3333333333333333
2*4/3
=> 2
2*4.0/3
=> 2.6666666666666665
This is because of something called Integer Division. Basically this means that if you divide two integers, the resulting number must be an integer. So, for example 3/4 would result in 0 and 4/3 would result in 1. This is because 3/4 in "real" math would give you 0.75 and to turn 0.75 into and integer, Python truncates the floating point values and leaves you with 0.
The easiest way to fix this is to use 4.0 instead of 4. Turning the integer into a float and disregarding Integer Division because integer divided by float results in a float. 3/4.0 equals 0.75 like you want it to.
Arithmetic with integer operands has an integer result.
>>> 3 / 2
1
I am looking for a way to perform a digit divided by larger value(2/5000000) and then store that value in table, but the problem is when i save that value, only 0 is stored , instead of correct value.I tried with float, double precision, but still only 0 is stored, is there any other way .
Thank you
Remember to operate on floating numbers, and not convert it after the operation. E.g. 2/5000000.
Also, use the Decimal library, if you are looking for more accurate decimals.
You need to use floating point division. To be explicit, you can cast ints to float:
>>> a = 2
>>> b = 5000000
>>> c = a/float(b)
>>> c
4e-07
You can cast either a or b to float.
In my application I encountered the following and was surprised by the results:
8/-7=-2 (both integers).
What does this mean?
For the actual values, i.e. 8.0/(-7.0), the result is roughly -1.143.
Your result using integer division is being rounded down toward the more negative value of -2. (This is also known as "Floor division")
This is why you will get the somewhat perplexing answers of:
>>> 8/(-7)
-2
>>> 8/7
1
Note: This is "fixed" in Python 3, where the result of 8/(-7) would be -1.143. So if you have no reason to be using Python 2, you should upgrade. ;)
In Python 3, if you still want integer division, you can use the // operator. This will give you the same answer as 8/(-7) would in Python 2.
Here's a Python Enhancement Proposal on the subject: PEP 238 -- Changing the Division Operator
Python always does the "floor division" for both negative numbers division and positive numbers division.
That is
1/10 = 0
1/-10 = -1
But sometime we need 1/-10 to be 0
I figure out it can be done by using the float division first then cast result to int, e.g.
int(float(1)/-10) = 0
That works fine for me, no need to import the future division or upgrade to Python 3
Hope it can help you~
To have Python automatically convert integer division to float, you can use:
from __future__ import division
Now:
8/-7=-1.1428571428571428
This feature is not in the standard Python 2 not to break existing code that relied on integer division.
However, this is the default behavior for Python 3.
When both values are integers when dividing Python uses Floor division.
In Python, / operator is for integer division. You can look at it as float division followed by a floor operation.
For example,
8/7 == floor(8.0/7.0) == 1
8/-7 == floor(8.0/-7.0) == -2