Python lossy conversion from float to int - python

I am trying to figure out a way to write 21*(10^21) in Python. I've tried two approaches. The first one is simply by doing the following:
>>> print(21*(10**21))
21000000000000000000000
This works just fine. However, the problem I'm trying to solve requires me to get to this number by iteration, i.e, by building up from 1*(10^1) all the way to 21*(10^21). So I tried the following:
>>> temp = 20*(10**20)
>>> print(21*temp*10/20)
2.1e+22
Now, I want the entire number to show up, and not in the 'e' form, so I converted it to int. But this prints the wrong answer:
>>> print(int(21*temp*10/20))
20999999999999997902848
I know that integers don't have a limit in Python 3 (which is what I'm using) so this baffles me. I thought this may be because the /20 part causes conversion to float, but the number 21*(10^21) falls within the limits of float, so converting back to int shouldn't be a problem.
I've tried searching for this error online with no luck. Any help would be appreciated.

Related

Assigning and reading big numbers in python give different results

I was working on some data and I had to assign a big number to some values in a DataFrame. Then I tried reading these values but surprisingly they changed. I know for a fact it's not a printing display problem but it's something different. Here is what i got as an example:
x = 410121209151013.6360
print("%.5f" % x)
And this is what I get :
410121209151013.62500
I made some tests and found out that there is some sort of a digit limitation but don't know how to fix it. Any help is much appreciated.
Floating-point math is fraught with peril related to how the numbers are stored (see comment on Question)
Whenever you can work in an integer space, try to do so, and then represent the numbers as you see fit (for example, you could multiply 'em by 1000000 and convert the units to milli-whatevers from Mega-whatevers)

Integer variables not storing in flatbuffers using python

I am seeing a strange issue using flatbuffers on python. I am trying to store a variable value which is integer, however, when I generate the JSON from the resulting .bin file the key / integer value will not be there.
However, if I simply pass an integer it works fine?
Do I need to do some kind of cast or generate an integer type in the same way as I do for strings?
Here is an example of the code I am running:
varInt = 1
SomeClass.SomeClassStart(my_builder)
SomeClass.SomeClassAddMyValue(my_builder, varInt)
SomeClass.SomeClassAddMyOtherValue(my_builder, 2)
some_class_buffer = SomeClass.SomeClassEnd(my_builder)
which produces json of :
some_class:{
my_other_value: 2
}
I don't understand why one way would work and one wouldn't? I don't have much experience with python and flatbuffers are completely new to me so not sure if theres some nuance that I'm unaware of?
Any help is appreciated.
I found the issue with this. It turns out that I had to set the builder to have the forceDefaults variable set to true.
my_builder.forceDefaults = True

Why do values values in a dataframe change when converting from int to float and back?

I am getting very strange behavior for a series of SKUs in my code when using "astype(float)" and "astype(int) and I am at a loss to explain why. This seems to only happen on my local machine (I couldn't duplicate it in an online Juypter Notebook).
Here is a list of products where this problem occurs and the DF I am creating with them:
products = {'SKU': [1111000120,1111000160,1111000182,1111000210,1111001300,2412601027,
2412601449,5172100236,5172100370,5172100713,7130104717]}
dfprod = pd.DataFrame.from_dict(products)
when I convert this df to fload and then back to int on my local machine I get the following: Conversion error
I found this question that treats a similar problem but is about C++ so I'm not too sure how applicable it is.
sign changes when going from int to float and back

Long Object in Python

I have a large CSV that is a result of a python script I wrote. Each row contains a list of entries, that when I wrote were strings or ints. Please note that the files from my script are sometimes created on either linux or windows platform (which might be the problem, hence the mention. I'm new at multi-platform python, so please forgive me).
Now, I'm trying to read the .csv in but some of the ints come in as long objects, according to type(whatiwant). I've tried everything I and my google fu can think of to convert these objects to int (int(), str(). replace for " ", "L", and "/r", "/n"). Nevertheless, when I test the list via for loop and type(), output says some things are still long objects.
What am I missing here? I tried looking for background info on long objects but couldn't find anything useful, hence the post.
I'm new at all this, so again, please forgive my ignorance.
When it rains, it pours. Sorry for screwing up the edit rofl:
I'm reading in the values like this (which are writte in rows, as a list containing values that are ints and strings):
Input = [["header"|"subheader"], [15662466|2831811638],
[5662466|27044023]...]
data = []
people_list = []
for entry in input:
data.append(entry)
for row in data:
holder = row.split("|")
person = str(holder([1])
people_list.append(person.replace.("\r", "").replace("\n","").replace("L", "")
people_list.pop(0)
for person in people_list:
strperson = str(person)
intperson = int(strperson)
print intperson
print type(intperson)
output:
2831811683
<type 'long'>
27044023
<type 'int'>
They are being treated as longs. Python as two number types: ints, which have a maximum and minimum value, and longs, which are unbounded. It's not really a problem if the numerical data is a long instead of an int.
A long is a datatype that is just longer than an int.
More formally long is the datatype used when integers would have caused an integer overflow, so anything more than sys.maxint automatically converts your int to a long.
Docs: https://docs.python.org/2/library/stdtypes.html#typesnumeric
Note that in Python 3 there is no significance between the two, as Python3 unifies the two types.

TypeError: SetPoint argument 1: integer argument expected, got float

can someone help me with this?
pts.InsertPoint(fl[i+1][j+1][k+1], xx[0][i+1], yy[0][j+1], zz[0][k+1])
TypeError: InsertPoint argument 1: integer argument expected, got float
fl is supposed to have float, I have the array from real world experiment, I can't change the values to int.
Is this an issue related to InsertPoint, is it only taking int? Can someone help me fix it?
Also, I don't have experience in Python, never wrote a program, this is the first program I am working with, I made changes to an old program to get it work for my purposes, but can't figure out what I did wrong
Thanks :)
If you look at the documentation of vtkpoints::InsertPoint you will see that the expected arguments are (id,x,y,z). You use this method when you have to want to set the value of the point at position id (that's why it must be an integer). http://www.vtk.org/doc/nightly/html/classvtkPoints.html#ab7f990c73fb291737abe4203994ce2a2
from the python shell, you can also check help(pts.InsertPoint) - but since these are wrapped objects sometimes the help appears a bit obscure.
The method InsertNextPoint, instead, just requires x,y,z and can be used as you are doing. It doesn't require an explicit id because it will just your point at the end
Specifically for python , you could be interested also in vtk.util.numpy_support which makes conversions between numpy and vtk elements easier (you can convert your points from numpy to a vtkdoublearray, then assign it to a vtkpoints with the method setdata )
The problem is not whether or not fl is composed of floats. The problem is that the method signature for InsertPoint expects each value to be an int.
From the python documentation:
exception TypeError:
Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.
This information is clearly available in the error message you pasted - it has both the exception type (TypeError) and the object by which it was thrown (InsertPoint). Had you accidentally included a float in your array indexers, the exception would have been thrown by something other than InsertPoint.
In the future, you should do some research on Google based on your exception error. I'm not telling you anything you couldn't find there.
If you still want to use InsertPoints, you have two options:
You can use the (int) cast to truncate edit: the first of your arguments to the whole, integer value (1.2 truncates to 1, 2.67 to 2, etc.), like so:
int(fl[i+1][j+1][k+1])
You can do #1, but round the value to the nearest integer value, like below (keep in mind that you'll need to import Math):
int(Math.Round(fl[i+1][j+1][k+1]))
Try wrapping each or your values in int(), like so:
pts.InsertPoint(int(fl[i+1][j+1][k+1]), int(xx[0][i+1]), int(yy[0][j+1]), int(zz[0][k+1]))

Categories