473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The smallest and largest values of numeric types

tom
Hi!
How can I determine the smallest and largest values of numeric types
(for example int) possible in my system? I think there exists a function
for this task but I don't know it.
Apr 17 '07 #1
9 3055
In article <QA************ **@read3.inet.f i>, to*@finland.com wrote:
Hi!
How can I determine the smallest and largest values of numeric types
(for example int) possible in my system? I think there exists a function
for this task but I don't know it.
There is or was a module called "kinds" which was an implementation of
PEP 0242. I have it and it gives the values you are looking for (and
more). But I don't know where I got it. I think it was once
distributed with Macpython on MacPython.org, but I'm not sure. I've
searched for it, but nothing shows up except my own questions about it
from many years ago and they have no clues.

Does anyone know where this package is? Or what might replace it. It
is very useful.
Apr 17 '07 #2
Lou Pecora wrote:
In article <QA************ **@read3.inet.f i>, to*@finland.com wrote:
>Hi!
How can I determine the smallest and largest values of numeric types
(for example int) possible in my system? I think there exists a function
for this task but I don't know it.

There is or was a module called "kinds" which was an implementation of
PEP 0242. I have it and it gives the values you are looking for (and
more). But I don't know where I got it. I think it was once
distributed with Macpython on MacPython.org, but I'm not sure. I've
searched for it, but nothing shows up except my own questions about it
from many years ago and they have no clues.
I'm surprised they also didn't turn up my replies to those questions.
Does anyone know where this package is? Or what might replace it. It
is very useful.
It used to be distributed with Numeric.

http://numpy.cvs.sourceforge.net/numpy/kinds/

numpy exposes the same information for floating point types with its finfo
class. Nothing particular for ints.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 17 '07 #3
On Apr 17, 7:12 am, t...@finland.co m wrote:
How can I determine the smallest and largest values
of numeric types (for example int) possible in my
system? I think there exists a function for this task
but I don't know it.
This should work for ints:

import sys
print sys.maxint
For floats, just try until it fails. This is from
http://projects.amor.org/geniusql/br...dapters.py#l41

# Determine max binary digits for float on this system. Crude but
effective.
maxfloat_digits = 2
while True:
L = (2 ** (maxfloat_digit s + 1)) - 1
if int(float(L)) != L:
break
maxfloat_digits += 1

Note this will get you the number of binary digits, which is not a
simple matter to translate to maximum and minimum values, since floats
are inexact numerics. IEEE 754 floats are stored with one bit for the
sign, some bits for the exponent (which is biased) and some for the
significand. See http://babbage.cs.qc.edu/IEEE-754/32bit.html for a
visual example of how it works.
Robert Brewer
System Architect
Amor Ministries
fu******@amor.o rg

Apr 17 '07 #4
tom
Thank you for your answers. Seems like the limits of numeric values
aren't considered as important in Python as in C ;)
Apr 17 '07 #5
fumanchu wrote:
On Apr 17, 7:12 am, t...@finland.co m wrote:
>How can I determine the smallest and largest values
of numeric types (for example int) possible in my
system? I think there exists a function for this task
but I don't know it.

This should work for ints:

import sys
print sys.maxint
One should note that ints bigger than sys.maxint are possible almost
seamlessly since Python will use longs for these numbers:

Python 2.5 (r25:51908, Mar 13 2007, 08:13:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright" , "credits" or "license" for more information.
>>import sys
sys.maxint
2147483647
>>sys.maxint+ 1
2147483648L
>>sys.maxint* 2
4294967294L
>>sys.maxint**1 0
208592483005316 931156432119130 593119974171156 068820005046395 057804716416933 772965076580224 2049L

Of course performance decreases for longer longs.
--
Michael Hoffman
Apr 17 '07 #6
On Apr 17, 11:37 am, t...@finland.co m wrote:
Thank you for your answers. Seems like the limits of numeric values
aren't considered as important in Python as in C ;)
Sure, they're important, we just don't want to notice them. That's why
conversion to longs is automatic, so that number size limits don't
cause problems and your problems are solved rather than throw
exceptions or produce invalid results.

Apr 17 '07 #7

"Michael Hoffman" <ca*******@mh39 1.invalidwrote:
>
208592483005316 931156432119130 593119974171156 068820005046395 057804716416933 77296
50765802242049L
>
Of course performance decreases for longer longs.
I once made a thing that tried to find the limit of longs and stopped
when I had two or three screenfulls of numbers.

I came to the conclusion that for "integer" arithmetic like this,
the limit is either your memory size, or some other number
that is so big that in practice you don't have to worry about it..

- Hendrik

Apr 18 '07 #8
On Wed, 18 Apr 2007 07:15:11 +0200, Hendrik van Rooyen wrote:
I once made a thing that tried to find the limit of longs and stopped
when I had two or three screenfulls of numbers.
You should find that converting longs to strings (say, for printing them)
is *very* slow. E.g. the following code

big = 10L**100 # one google, a big number
while True:
print big # so we can see the last value before it fails
big *= 10

will run terribly slow and should be written as:

big = 10L**100 # one google, a big number
try:
while True:
big *= 10
except: # don't know what exception will be raised, so catch ANYTHING
print len(str(big))-1 # the number of digits

only does the slow conversion to string once, instead of every time around
the loop. However, once your machine starts paging, it will still slow
down a lot.

I came to the conclusion that for "integer" arithmetic like this, the
limit is either your memory size, or some other number that is so big
that in practice you don't have to worry about it..
Yes, longs are limited only by the amount of memory accessible.
--
Steven D'Aprano

Apr 18 '07 #9
On Apr 18, 3:09�am, Steven D'Aprano <s...@REMOVEME. cybersource.com .au>
wrote:
On Wed, 18 Apr 2007 07:15:11 +0200, Hendrik van Rooyen wrote:
I once made a thing that tried to find the limit of longs and stopped
when I had two or three screenfulls of numbers.

You should find that converting longs to strings (say, for printing them)
is *very* slow. E.g. the following code

big = 10L**100 # one google, a big number
while True:
* * print big # so we can see the last value before it fails
* * big *= 10

will run terribly slow and should be written as:

big = 10L**100 # one google, a big number
try:
* * while True:
* * * * big *= 10
except: # don't know what exception will be raised, so catch ANYTHING
* * print len(str(big))-1 # the number of digits

only does the slow conversion to string once, instead of every time around
the loop. However, once your machine starts paging, it will still slow
down a lot.
I came to the conclusion that for "integer" arithmetic like this, the
limit is either your memory size, or some other number that is so big
that in practice you don't have to worry about it..

Yes, longs are limited only by the amount of memory accessible.
But there may be other limitations even if you have the memory.

For example, this test is limited to generation 10
because tne 11th generation produces "outrageous
exponent" error. Here, every 9th 1st generation,
starting from the fifth is a second generation, every
9th sencond, starting from the fifth, is a 3rd generation,
every 9th 3rd gen, starting from the 5th is a 4th gen, etc.

Closed form: Type12MH(k,i)
Find ith, kth Generation Type [1,2] Mersenne Hailstone
using the closed form equation

2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)/2+1)-1)-1

2**5-1 generation: 1
2**29-1 generation: 2
2**245-1 generation: 3
2**2189-1 generation: 4
2**19685-1 generation: 5
2**177149-1 generation: 6
2**1594325-1 generation: 7
2**14348909-1 generation: 8
2**129140165-1 generation: 9
2**1162261469-1 generation:10

1.797 seconds

There is never a number too large to worry about.
>
--
Steven D'Aprano

Apr 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8882
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
4
6478
by: Code4u | last post by:
I need to write an algorithm that sheds the outliers in a large data set, for example, I might want to ignore the smallest 2% of values and find the next smallest. Boost has a nth_element algorithm, however, it partially sorts the data. I have a requirement that the data remain in the orginal order. Of course I could make a copy of the vector or array and then apply nth_element, but this would be expensive in memory and would require...
11
2373
by: dmsy | last post by:
Is there a way to use javascript to increase the overall document text size, just like if you click View->Text Size->Largest, Larger, Medium, Smaller, Smallest? This is on IE browser.
3
6518
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and 123234, I want 4236 to be second because 4 is bigger than 1 rather than using the numeric comparison. The strings can include character values and strings. Basically, I have the bubble sort function, the question is how to compare those types of...
8
10404
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have is not much help here either. Googling has given me a little help. This is my current understanding -- I would appreciate any comments or corrections... "" -- this means an empty string when applied to String data type, and also to Variant...
13
5140
by: Peter Ammon | last post by:
I have a floating point number. I'd like to get the nearest floating point number that is larger or smaller than the given number. I investigated FLT_EPSILON but it only seems to be useful if the given number is 1. Any suggestions? Thanks, -Peter
90
3410
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
4
47672
by: MBeckford05 | last post by:
Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison operate determine which number is larger and which is smaller. I am new to java I was wondering how to but this into java source code. Can anyone Help? Thank You. MBeckfod05
8
12171
crystal2005
by: crystal2005 | last post by:
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong.... Can anyone help me out?? #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_INPUT 25
0
8319
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.