How to get the minimum number that can be represented?

P

Peng Yu

Hi,

Suppose I want to define a function that return the minimum number
that can be represented.

def f(x):
#body

That it, if I call f(10), f will return the minimum integer that can
be represented in the machine; if I cal f(10.5), f will return the
minimum float that can be represented in the machine.

Could somebody let me know what should be in the function body?

Regards,
Peng
 
D

Daniel Fetchinson

Suppose I want to define a function that return the minimum number
that can be represented.

def f(x):
#body

That it, if I call f(10), f will return the minimum integer that can
be represented in the machine; if I cal f(10.5), f will return the
minimum float that can be represented in the machine.

Could somebody let me know what should be in the function body?

I'm not sure this is what you are looking for but have a look at

import sys
print sys.maxint

HTH,
Daniel
 
P

Peng Yu

I'm not sure this is what you are looking for but have a look at

import sys
print sys.maxint

The problem is how to know what type of the argument and call the
corresponding function.

In C++, it is relatively easy (<limits> header). Is there an
equivalent in python?

Regards,
Peng
 
G

Grant Edwards

Suppose I want to define a function that return the minimum number
that can be represented.

def f(x):
#body

That it, if I call f(10), f will return the minimum integer that can
be represented in the machine; if I cal f(10.5), f will return the
minimum float that can be represented in the machine.

Could somebody let me know what should be in the function body?

The stuff you wan is in the "sys" module.

For example:
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024,
max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021,
min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)
2147483647

You might also want to read up on the type() builtin
 
P

Peng Yu

The stuff you wan is in the "sys" module.

For example:

sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024,
max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021,
min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)

2147483647

You might also want to read up on the type() builtin

I want avoid using any 'if' statement. In C++, I can use template. How
to do not use 'if' statement in python?

Regards,
Peng
 
G

Grant Edwards

I want avoid using any 'if' statement.
Why?

In C++, I can use template.

If you want to write C++ code, then you should use a C++
compiler.
How to do not use 'if' statement in python?

In python you use the 'if' statement.
 
D

Dave Angel

Peng said:
I want avoid using any 'if' statement. In C++, I can use template. How
to do not use 'if' statement in python?

Regards,
Peng
So if the homework assignment is for C++, do it in C++. If this is
supposed to be some kind of programming challenge, then you need to
state all the rules up front. For example, you could write something like

def f(x):
return [-10, 42][2*x - 20]

and it'll return -10 for value 10 and 42 for value 10.5

Or:

def f(x):
result = raw_input("What's the smallest value of the same type as "
+ str(x))
return result

And of course if the constraint is not to use the if statement, and you
can use Python 2.6, how about a conditional expression?
 
S

Simon Forman

I want avoid using any 'if' statement. In C++, I can use template. How
to do not use 'if' statement in python?

Regards,
Peng

Python polymorphism is done differently than C++ templates.

Normally you would write your code to be as "type agnostic" as you
reasonable could. In this case you actually desire to do different
things based on the type of the argument. Either use an if statement
or you can dispatch on the type using a dict:


mins = {
float: 2.2250738585072014e-308,
int: 0,
long: 0,
}

def f(arg):
return mins[type(arg)]

(Hint: google for "duck typing".)
HTH,
~Simon
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top