I'm trying to take a number and divide it by 100 to get 1%, but when i tried to convert it to integer using the int(), it's giving me some weird output. i have no clue what i'm doing wrong here.
totalsupply = 1000000000000000000000000000000
onepercent = int((totalsupply/100))
print(totalsupply)
print(onepercent)
the output is coming out as such:
1000000000000000000000000000000
9999999999999999583119736832
[Finished in 68ms]
I was expecting the onepercent to be this: 10000000000000000000000000000.
According to this post, python tries to convert the number to a float on a division. However, floats are limited to 1.7976931348623157e+308. A workaround is to use the // operator which returns an int from the division, so for your example:
totalsupply = 1000000000000000000000000000000
onepercent = totalsupply//100
print(totalsupply)
print(onepercent)
Python has a built-in integer type that has infinite precision. That's what you are creating with your first line
totalsupply = 1000000000000000000000000000000
However, python has two division operators. The standard division "slash" operator ("/") is floating-point operation, whereas the double-slash gives integers.
Hence totalsupply/100 is a floating point number, which has finite precision, and so the closest it can do is 9999999999999999583119736832.
However, if instead you do
onepercent = totalsupply//100
print(onepercent)
you get the expected 100000000000000000000000000000.
Related
My dataframe has columns where one has list of float values. When I train that column as X_train, I showing cannot string to float or tensorflow float data type.
DataSet:
I tried this:
df['sent_to_vec'].apply(lambda x: float(x))
or nested for loop to convert values in float type; but didn't get executed.
Try passing a string that's really just a floating-point number to the Python float() function:
f1 = float('0.1')
print(f1)
It works.
Try passing a string that's not just a floating-point number, but is instead some sort of array or list representation with multiple numbers separated by other punctuation:
f2 = float('[0.1, 0.2]')
print(f2)
You'll get the same error as you're asking about. That string, '[0.1, 0.2]' is not a representation of a floating-point number that float() can read.
You should look for a function that can read a string like '[0.1, 0.2]'. Can you see the code that wrote the Vectorized data.csv file? (Did you write that code, or that file?)
You'll want to use some function that does the reverse of whatever wrote that column of the file.
I am trying to convert decimal geographic coordinates as strings to a float.
The coordinates are in a csv like this '51213512'. With my Python script I am just reading the coordinates and add the '.'. If I am not adding the comma the rest of my script isn't working.
I already tried a few things but nothing worked for me. This is what I got so far.
latitude=float(long('51.213512'))
The Result is a ValueError:
ValueError: invalid literal for long() with base 10: 'Long'
not too sure why you are using long in this examply if you want to convert this variable to a float just use the float function on its own, you seem to be confusing the long and float functions. dont use both you will be confusing python (basically dosent know what to do because your giving it 2 arguments at once)
I recommend just using the float function on its own. This will avoid confusion
latitude = float('51.2135512')
Get rid of the 'long' and it should work
latitude = float('51.213512')
Edit: Okay, since you're getting the coordinates and manually converting to decimal strings, all you need to do is use the code I said originally. The long function
converts integers or strings of integers to long types, not float types.
>>> long(5)
5L
>>> long('5')
5L
>>> long(5.5)
5L
>>> long('5.5')
ValueError: invalid literal for long() with base 10: '5.5'
When creating a String object in Swift you can use a String Format Specifier to convert an integer to hexadecimal notation.
print(String(format:"%x", 1234))
// output: 4d2
// expected output: 4d2
But when numbers become bigger, the output is not as expected.
print(String(format:"%x", 12345678901234))
// output: 73ce2ff2
// expected output: b3a73ce2ff2
It seems that the output of String(format:"%x", n) is truncated at 8 characters. I don't think in hexadecimal natively, this makes debugging hard. I have seen answers for other programming languages where it is explained that you need to brake-up the large integer into parts, but that seems wrong to me.
What am I doing wrong here?
What is the right way to convert decimal numbers to hexadecimal numbers in Swift?
You need to use %lx or %llx
print(String(format:"%lx", 12345678901234))
b3a73ce2ff2
Table 2 on the site you linked specifies them
l -
Length modifier specifying that a following d, o, u, x, or X conversion specifier applies to a long or unsigned long argument.
x is for unsigned 32 bit integers which only go up to 4.294.967.296
Iam working in python 3.6
I receive from serial comunication an string '3F8E353F'. This is a float number 1.111. How can I convert this string to a float number?
Thank you
Ah yes. Since this is 32-bits, unpack it into an int first then:
x='3F8E353F'
struct.unpack('f',struct.pack('i',int(x,16)))
On my system this gives:
>>> x='3F8E353F'
>>> struct.unpack('f',struct.pack('i',int(x,16)))
(1.1109999418258667,)
>>>
Very close to the expected value. However, this can give 'backwards' results based on the 'endianness' of bytes in your system. Some systems store their bytes least significant byte first, others most significant byte first. See this reference page for the descriptors to format based on byte order.
I used struct.unpack('f',struct.pack('i',int(x,16))) to convert Hex value to float but for negative value I am getting below error
struct.error: argument out of range
To resolve this I used below code which converts Hex (0xc395aa3d) to float value (-299.33). It works for both positive as well for negative value.
x = 0xc395aa3d
struct.unpack('f', struct.pack('I', int(x,16) ))
Another way is to use bytes.fromhex()
import struct
hexstring = '3F8E353F'
struct.unpack('!f', bytes.fromhex(hexstring))[0]
#answer:1.1109999418258667
Note: The form '!' is available for those poor souls who claim they can’t remember whether network byte order is big-endian or little-endian (from struct docs).
I would like to ask something about data types in Lua.
I get from serial link some message (command:value) like this:
tmp_string = "BRAKE:1"
then I parse this string to command and value in two different functions (one is for command and other one is for value). This is function for parsing value
function parser(value)
index = string.find(value, ":")
result = value.sub(value, index+1)
return result
end
I would like to now what sort of data type result is? If I use string match it works.
...if string.match(state, "1") then...
However it also works when I do something like this
x = (state*65536)/3.2808)
I thought the result is string, but I don't understand why it works also with numerical operations. Thank you in advance.
Lua 5.3 Reference Manual, §3.4.1 - Arithmetic Operators
With the exception of exponentiation and float division, the arithmetic operators work as follows: If both operands are integers, the operation is performed over integers and the result is an integer. Otherwise, if both operands are numbers or strings that can be converted to numbers (see §3.4.3), then they are converted to floats, the operation is performed following the usual rules for floating-point arithmetic (usually the IEEE 754 standard), and the result is a float.
Emphasis is mine.
When dealing with operations, Lua will attempt to convert string operands to floats, and if it works - it works. If it fails, you get an error.
>| '55' / 2
<| 27.5
>| 'foo' / 2
<| error: [string "return 'foo' / 2"]:1: attempt to perform arithmetic on a string value
If you want to be explicit about this (and safe) use tonumber, and handle the nil-case.
If you need to know the type of a value in Lua, you can pass the variable to type and check the resulting string.