How to get the minimum number that can be represented?

Dave Angel davea at ieee.org
Sun Sep 20 21:30:32 EDT 2009


Peng Yu wrote:
> On Sun, Sep 20, 2009 at 9:37 AM, Grant Edwards <invalid at invalid.invalid> wrote:
>   
>> On 2009-09-20, Peng Yu <pengyu.ut at gmail.com> wrote:
>>
>>     
>>> 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.float_info
>>>>>           
>> sys.floatinfo(max=7976931348623157e+308, max_exp24,
>> max_10_exp08, min=2.2250738585072014e-308, min_exp=-1021,
>> min_10_exp=07, dig, mant_digS, epsilon=2.2204460492503131e-16, radix=2, rounds=1)
>>
>>     
>>>>> sys.maxint
>>>>>           
>> 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
>
>   
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?





More information about the Python-list mailing list