I have a python method that calculates value and returns it.
The value is
CalcData = 3941269503L.
It is only 32 bit. Yet this is typecasted to long implicitly (suffixed by L) and when I access this method in COM in other application , I get the Python int too large to convert to C long error. I even tried typecasting it into int,but no luck
The "long" C type is signed so the largest positive value it can store is 2,147,483,647. The error is indeed correct, it doesn't fit.
Try "unsigned long" and "UL" postfix on the constant.
Related
For quite a long time I can not find a solution to this problem on the Internet. I must say that it is impossible to change the data type for a specific field, since the product has already been released and there is no way to replace the decoding of the Protobuf message or completely abandon Protobuf :(
The problem is that the Protobuf data type, number 5, accepts only the standard Python int, which may be too large due to the fact that it is not an unsigned int.
I tried using numpy. uint32 and ctypes. c_uint32, but both options end up being converted automatically to regular Python int and I get the error: "ValueError: Value out of range: 2902080034". I need to somehow fit in the 5th Protobuf data type the first 32 bits of UUID4 as a number.
I hope for help, thank you in advance. Unfortunately, in my opinion, this may be a popular problem not described here before.
Type .proto uint32 corresponds to python int, i.e. it has range -2147483648 through 2147483647.
One solution is to convert your unsigned 32 bit value to a signed one by:
if (value & 0x80000000):
value = -0x10000000 + value
The conversion is as suggested here:
How to get the signed integer value of a long in python?
I have a task to calculate a sum of very big integers.
the sum could be over the limit of max value of int.
I'd like to use long long int type like in c to prevent this.
python is dynamic type language.
but there must be someway to declare this long long int type.
help me to declare long long int type in python.
thanks
You don't need to.
>>> sum([
... 1243926478235632786572938657832682396538279658237956832976482375678239659782365,
... 23590237589734985720423803758031640192748372946743079324780137092704730297409327409237409237432,
... ])
23590237589734986964350281993664426765687030779425475863059795330661563273891703087477069019797
Python has arbitrary-size integers, that can become way larger than C's long long.
I tried bith ways using map nad using for loop but its not working i know for for loop it has to list,tuples or string. So how do i make this work
1
def narcissistic(value):
x = ((value)== sum((c)**len(value) for c in list(value)))
return x
2
def narcissistic(value):
x=(value== (map(lambda c :sum(c**len(value)),value)))
return x
Your issue comes down to confusion about the type of your different objects. Python is a strongly typed language, so each object has a clear type at any given moment and the language generally won't convert anything to another type automatically for you.
Based on the error you're getting, you're calling your function with an int argument. This causes you trouble when you try to call len or iterate on your value. Python ints don't have a length, nor are they iterable, so it's quite understandable that these fail under the circumstances.
What you want to do is create a string representation of your value number. Then you can loop over the characters of the string, and take its len freely.
There's another issue though. You're also trying to do an exponential operation on the c variable in the generator expression. That won't work because c is a string, not a number. It's a one-digit string, but still a str instance! To do math with it, you need to convert it back to a number with int.
Here's a fixed version of your function:
def narcissistic(number):
num_str = str(number)
return sum(int(c)**len(num_str) for c in num_str) == number
I've renamed the very generic value name with number, which should hopefully make it more clear what type each thing is.
so far this is what i found:
from random import randint
randint(100000000000000000000, 999999999999999999999)
the output is:
922106555361958347898L
but i do not want that L there..
i can only use this as an int if there is no "L" there at the end of it.
UPDATE
would it be a better idea to generate two small numbers and then combine them if the goal is to simply have a random number that is 30 digits long ?
The reason there's an L there is because this is too large to fit into an int,* so it's a long. See Numeric Types — int, long, float, complex in the docs for more details.
So, why do you get that L, and how do you get rid of it?
Python has two different ways to turn a value into a string representation:
repr either returns the canonical source-code representation, or something like <__main__.Eggs at 0x10fbd8908>).
str returns a human-friendly representation.
For example, for strings, str('abc') is abc, while repr('abc') is 'abc'.
And for longs, str(1L) is 1, while repr(1L) is 1L.
When you just type an expression at the interactive prompt, it uses repr. But when you use the print command, it uses str. So, if you want to see the value without the L, just print it:
print randint(100000000000000000000, 999999999999999999999)
If you want to, e.g., save the string in a variable or write it to a file, you have to call str explicitly.
But if you just want to use it as a number, you don't have to worry about this at all; it's a number, and int and long values can be intermixed freely (as of Python 2.3 or so).
And if you're trying to store it in a MySQL database, whichever MySQL interface you use won't care whether you're giving it int values or long, as long as they fit into the column type.**
Or you could upgrade to Python 3.x, where there is no separate long type anymore (all integers are int, no matter how big) and no L suffix.
* The exact cutoff isn't documented anywhere, but at least for CPython, it's whatever fits into a C long on your platform. So, on most 64-bit platforms, the max value is (1<<63)-1; on the other 64-bit platforms, and all 32-bit platforms, it's (1<<31)-1. You can see for yourself on your platform by printing sys.maxint. At any rate, your number takes 70 bits, so unless someone ports Python 2.x to a platform with 128-bit C longs, it won't fit.
** Note that your values are too big to fit into even a MySQL BIGINT, so you're going to be using either DECIMAL or NUMERIC. Depending on which interface you're using, and how you've set things up, you may have to convert to and from strings manually. But you can do that with the str and int functions, without worrying about which values fit into the int type and which don't.)
If you're on the interactive prompt, explicitly print the value. The repr of the value has an L, but the str of the value doesn't.
>>> 922106555361958347898
922106555361958347898L
>>> print 922106555361958347898
922106555361958347898
The output in the REPL has an L suffixed; if you print the value, it is not displayed.
>>> from random import randint
>>> print randint(100000000000000000000, 999999999999999999999)
106315199286113607384
>>>
Python provides a convenient method long() to convert string to long:
long('234')
; converts '234' into a long
If user keys in 234.89 then python will raise an error message:
ValueError: invalid literal for long()
with base 10: '234.89'
How should we a python programmer handles scenarios where a string with a decimal value ?
Thank you =)
longcan only take string convertibles which can end in a base 10 numeral. So, the decimal is causing the harm. What you can do is, float the value before calling the long. If your program is on Python 2.x where int and long difference matters, and you are sure you are not using large integers, you could have just been fine with using int to provide the key as well.
So, the answer is long(float('234.89')) or it could just be int(float('234.89')) if you are not using large integers. Also note that this difference does not arise in Python 3, because int is upgraded to long by default. All integers are long in python3 and call to covert is just int
Well, longs can't hold anything but integers.
One option is to use a float: float('234.89')
The other option is to truncate or round. Converting from a float to a long will truncate for you: long(float('234.89'))
>>> long(float('1.1'))
1L
>>> long(float('1.9'))
1L
>>> long(round(float('1.1')))
1L
>>> long(round(float('1.9')))
2L