More int and float attributes

B

bearophileHUGS

sys.maxint gives the largest positive integer supported by Python's
regular integer type. But maybe such attribute, with few others (they
can be called min and max) can be given to int type itself.
D is a very nice language, that I hope to see more used. It is copying
lot of things from Python. D Floating point values have some
proprieties:

http://www.digitalmars.com/d/property.html

Properties for Floating Point Types:
..init initializer (NaN)
..infinity infinity value
..nan NaN value
..dig number of decimal digits of precision
..epsilon smallest increment
..mant_dig number of bits in mantissa
..max_10_exp maximum exponent as power of 10
..max_exp maximum exponent as power of 2
..min_10_exp minimum exponent as power of 10
..min_exp minimum exponent as power of 2
..max largest representable value that's not infinity
..min smallest representable value that's not 0
..re real part
..im imaginary part

I think such attributes may be useful to be added to Python float
type/values too.

Bye,
bearophile
 
A

Avizoa

It seems as though just about all of those would be rarely, if ever,
used by the vast majority of programmers.

Plus, python already handles the two most important (NaN and complex)
well.
 
P

Paddy

sys.maxint gives the largest positive integer supported by Python's
regular integer type. But maybe such attribute, with few others (they
can be called min and max) can be given to int type itself.
D is a very nice language, that I hope to see more used. It is copying
lot of things from Python. D Floating point values have some
proprieties:

http://www.digitalmars.com/d/property.html

Properties for Floating Point Types:
.init initializer (NaN)
.infinity infinity value
.nan NaN value
.dig number of decimal digits of precision
.epsilon smallest increment
.mant_dig number of bits in mantissa
.max_10_exp maximum exponent as power of 10
.max_exp maximum exponent as power of 2
.min_10_exp minimum exponent as power of 10
.min_exp minimum exponent as power of 2
.max largest representable value that's not infinity
.min smallest representable value that's not 0
.re real part
.im imaginary part

I think such attributes may be useful to be added to Python float
type/values too.

Bye,
bearophile
The thing about float is that people expect it to be 'fast'. Most CPU's
have two or one representationfor floats that is supported by hardware
and lightning fast compared to a pure software implementation.
The decimal module provides for the 'bean counters' out their who can't
afford to round off a penny, but I must say that this is the first
request I have seen for a flexible float where you would not have
hardware support (when hardware float support is available).

Or do you mean the ability to choose between hardware supported float
s? e.g. float and double precision?
Or to be able to just interrogate the float implementation so your prog
can adjust to whatever implementation it is running under?
Something like:
assert float.mant_dig > 20


- Paddy.
 
B

bearophileHUGS

Paddy:
Or do you mean the ability to choose between hardware supported float
s? e.g. float and double precision?

No, I mean just having the ability to ask the float (his attribute)
what are the max and min values it can represent, etc.

stop = float.max
....

I don't know any simple way to know that values now. And importing sys
to know the max int looks stupid.

maxi = int.max
mini = int.min
....

Such attributes can be given to the Decimal values too, if you want.

Bye,
bearophile
 
T

Tim Roberts

sys.maxint gives the largest positive integer supported by Python's
regular integer type. But maybe such attribute, with few others (they
can be called min and max) can be given to int type itself.
D is a very nice language, that I hope to see more used. It is copying
lot of things from Python.

I don't see that. It looks rather like an incremental improvement to C and
C++ rather than a language influenced by Python.
D Floating point values have some proprieties:

http://www.digitalmars.com/d/property.html

Properties for Floating Point Types:
.init initializer (NaN)
.infinity infinity value
.nan NaN value
.dig number of decimal digits of precision
.epsilon smallest increment
...

There's an interesting philosophical difference here. D is defined as a
"systems programming language". It is compiled to native machine code,
like C. In such a case, the programmer necessarily needs to concern
himself with the processor's representation of integers and floats.

Python has a rather different focus. I certainly use it for system
programming tasks, but in most cases, a Python programmer shouldn't need to
worry about the internal representation of variables. Look, for example,
at the blurred distinction between integers and long integers.

I'm not arguing for or against the proposal, but I suspect these properties
would be rarely used.
 
P

Paddy

Paddy:

No, I mean just having the ability to ask the float (his attribute)
what are the max and min values it can represent, etc.

stop = float.max
...

I don't know any simple way to know that values now. And importing sys
to know the max int looks stupid.

maxi = int.max
mini = int.min
...

Such attributes can be given to the Decimal values too, if you want.

Bye,
bearophile
Hi bearophille,
decimals have their context; maybe floats could have your read-only
'context' values made available somehow. I don't know if their is an
issue with making this (static) data available from all float
instances, so maybe it should go in sys.

Maybe people, (me included), don't pay enough attention to the
trade-offs inherent in floating point arithmatic. I was taught about
difference equations, and rounding errors. I regularly compute answers
in programs without regard to floating point precision because the
defaults chosen work for most of my cases, but if I needed to be more
precise then I too would need some of the info you suggest.

Question: do the scientific packages supported by Python supply this
data in a regular manner?

- Paddy.

P.S. My appologies to any professional 'counters of currencies' out
their who may have been offended by my earlier use of the term 'bean
counters' - You probably earn much more than me - don't call in my
loans - I have babes in arms to feed.... :)
 
P

Paddy

Paddy said:
Hi bearophille,
decimals have their context; maybe floats could have your read-only
'context' values made available somehow. I don't know if their is an
issue with making this (static) data available from all float
instances, so maybe it should go in sys.

Maybe people, (me included), don't pay enough attention to the
trade-offs inherent in floating point arithmatic. I was taught about
difference equations, and rounding errors. I regularly compute answers
in programs without regard to floating point precision because the
defaults chosen work for most of my cases, but if I needed to be more
precise then I too would need some of the info you suggest.

Question: do the scientific packages supported by Python supply this
data in a regular manner?

- Paddy.

P.S. My appologies to any professional 'counters of currencies' out
their who may have been offended by my earlier use of the term 'bean
counters' - You probably earn much more than me - don't call in my
loans - I have babes in arms to feed.... :)

P.P.S. Just looking at the values you propose, if some of them can be
computed reliably from others (in the given float precision), then they
should NOT be included, but the module documentation should state how
they can be derived.
 
R

Robert Kern

Paddy said:
Question: do the scientific packages supported by Python supply this
data in a regular manner?

For floating point types, at least.

In [11]: from numpy import *

In [12]: print finfo(float32)
Machine parameters for <type 'float32scalar'>
---------------------------------------------------------------------
precision= 6 resolution= 1.0000000e-06
machep= -23 eps= 1.1920929e-07
negep = -24 epsneg= 5.9604645e-08
minexp= -126 tiny= 1.1754944e-38
maxexp= 128 max= 3.4028235e+38
nexp = 8 min= -max
---------------------------------------------------------------------


In [13]: print finfo(float64)
Machine parameters for <type 'float64scalar'>
---------------------------------------------------------------------
precision= 15 resolution= 1.0000000000000001e-15
machep= -52 eps= 2.2204460492503131e-16
negep = -53 epsneg= 1.1102230246251565e-16
minexp= -1022 tiny= 2.2250738585072014e-308
maxexp= 1024 max= 1.7976931348623157e+308
nexp = 11 min= -max
---------------------------------------------------------------------


--
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
 
B

bearophileHUGS

Self:
Tim Roberts:
I don't see that. It looks rather like an incremental improvement to C and
C++ rather than a language influenced by Python.

Thank you for your comments. Mine was probably just an illusion. In D
most things are copied or come directly from C++. But I like the built
in string and associative array management, the string/array slicing,
the multiple return or typed multiple input, the nested functions, the
foreach with a bit of type inferencing, and some other things that I
like in Python too, but absent in C++ (GCC has some nested functions,
etc).
(But D lacks some of the suggestions in this article, link coming from
Paddy:
http://www.cs.princeton.edu/~dpw/popl/06/Tim-POPL.ppt )

This is the first thing written about D into its site:
http://www.digitalmars.com/d/
D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python.<

This is a thread about related matters, with a comment from Walter, the
D autor:
http://www.digitalmars.com/d/archives/19839.html

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top