# Python Question about program options in if-clause

1. Oct 16, 2016

### late347

The task was to make python code which utilizes functions and main program.

the objective is to make function which takes two parameters (ostensibly number values) and outputs the smaller of the numbers.

I realized that the problem statement in a sense was incomplete because there is the possibility that with real numbers, the parameter numbers are equal size, and therefore equal. Therefore it is impossible that in such a case there exists a smaller number.

Question.
I think that I understood intuitively that if the one number is NOT smaller than the other number. And the one number is NOT bigger than the other number... I think probably the numbers are equal.
What is the mathematical or logical principle in which this reasoning is grounded (is it something like law of excluded middle). This thinking seems to work for real numbers on the number line, at least)
It also seems to work for integers, when both are integer. And indeed the set of reals, seems to include subset of integers.

However in python I thiink it is not allowed to put fraction into float type of input?

Code (Python):
def small(number1,number2):
if number1>number2:
return number2
elif number1<number2:
return number1
else:
same="the values which you gave are equal"
return same

a=float(input("tell first number"))
b=float(input("tell second number"))
result= small(a,b)
print(result)

2. Oct 16, 2016

### Staff: Mentor

You're overthinking this. In the case where the two numbers are equal, you are free to choose either number as the "smaller" of the two.
Not probably. Given two real numbers a and b. Then exactly one of the following relationships must be true:
a < b
a == b
a > b
If I recall correctly, it's called Archimedean trichotomy
Of course. If it works for real numbers, it has to work integers, which are a subset of the reals.
Do you mean a fraction like 1/2? If so, you are correct. 1/2 is not considered to be a floating point value in python or an any other programming language I can think of.
How it's almost always done:
Code (Python):
def smaller(number1, number2):
if number1>number2:
return number2
else:
return number1

a=float(input("tell first number"))
b=float(input("tell second number"))
result= smaller(a,b)
print(result)

3. Oct 16, 2016

### Staff: Mentor

One point to keep in mind is that you can't test for equality of floating point numbers without using some kind of tolerance as two floating point numbers may be equal to you but but have different bit representations.

Consequently, programmers would test equality using this scheme:
Code (Python):

tolerance = 0.000001

if( (a - b) < tolerance):
println("a equals b")

else if (a > b):
println("a is greater than b")

else:
println("a is less than b")

4. Oct 16, 2016

### Svein

You can say much more compressed in C:
Code (C):
#define MAX(a, b) ((a)>(b)) ? (a) : (b);

Last edited: Oct 18, 2016
5. Oct 16, 2016

### Ibix

There's a typo above, I think - should be abs(a-b)<tolerance.

6. Oct 16, 2016

### Staff: Mentor

Yes, you're quite right!

7. Oct 16, 2016

### Staff: Mentor

True, you can't test for exact equality, but you can test for "less than" or "greater than" with no unexpected results, so I don't see that tolerance is needed here (or abs() as was also suggested).

The function that late347 is a bit bothersome for me, as it returns either a number or a string. This kind of behavior isn't permitted in more strongly-typed languages, in which you have to specify the return type as some specific type.

8. Oct 19, 2016

### wle

You can write the comparably concise
Code (Python):
def max(a, b):
return a if a > b else b
in Python. There is no good reason to do this, though, since Python already provides min and max functions which work with arbitrary numbers of arguments as well as iterable objects (like a list of numbers) and generators. So for instance you could read a line of space-separated floating point numbers and print the smallest one with a line of code like this:
Code (Python):
print("Smallest number read was", min(map(float, input().split(' '))))