Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert double to float in Python

In a Python program, I have these two values:

v1 = 0.00582811585976
v2 = 0.00582811608911

My hypothesis is that v1 is a 64-bits floating point value, and v2 is v1 converted to a 32-bits floating point value. How can I verify this?

Details:
The first value comes from a hardware board that calculates with 64-bits precision. The board sends the value to a PC, but it should also convert the value to 32-bits precision and send that to another board, which in turn sends it to a PC. I just want to verify that this is really happening and all I have are two large arrays of numbers.

like image 259
compie Avatar asked Nov 08 '12 15:11

compie


People also ask

Can double be converted to float?

Using TypeCasting to Convert Double to Float in Java To define a float type, we must use the suffix f or F , whereas it is optional to use the suffix d or D for double. The default value of float is 0.0f , while the default value of double is 0.0d .

How do you convert to float in Python?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Is a double A float in Python?

Python does not have an inbuilt double data type, but it has a float type that designates a floating-point number. You can count double in Python as float values which are specified with a decimal point.


1 Answers

You can use the struct module to play with numerical representations:

import struct

>>> struct.unpack("f", struct.pack("f", 0.00582811585976))
(0.005828116089105606,)
like image 192
K. Brafford Avatar answered Sep 18 '22 13:09

K. Brafford