Question about program options in if-clause

  • Context: Python 
  • Thread starter Thread starter late347
  • Start date Start date
  • Tags Tags
    Options Program
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a Python function designed to determine the smaller of two numbers, addressing the nuances of equality among real numbers and the implications for programming. Participants explore the mathematical principles underlying comparisons, particularly in the context of floating-point representation and equality testing.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants note that if two numbers are neither smaller nor larger than each other, they must be equal, questioning the logical principle behind this reasoning.
  • Others clarify that this reasoning aligns with the Archimedean trichotomy, which states that for any two real numbers, one of three relationships must hold: less than, equal to, or greater than.
  • There is a discussion about the limitations of floating-point equality in programming, with some participants suggesting the use of a tolerance for comparisons due to potential differences in bit representation.
  • One participant points out a typo in a proposed equality check, suggesting it should use the absolute difference to account for floating-point precision issues.
  • Concise implementations of similar functionality in C and Python are shared, with some participants arguing that Python's built-in functions like min and max provide sufficient solutions without needing custom implementations.
  • Concerns are raised about the function returning different types (number or string), which may not align with practices in strongly-typed languages.

Areas of Agreement / Disagreement

Participants generally agree on the mathematical principles of comparison but express differing views on the implementation details and the handling of floating-point equality. The discussion remains unresolved regarding the best practices for implementing such comparisons in Python.

Contextual Notes

Limitations include the potential for confusion regarding floating-point representation and equality, as well as the implications of returning multiple data types from a function.

late347
Messages
300
Reaction score
15
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?
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)
 
Technology news on Phys.org
late347 said:
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.
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.
late347 said:
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.
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
late347 said:
What is the mathematical or logical principle in which this reasoning is grounded (is it something like law of excluded middle).
If I recall correctly, it's called Archimedean trichotomy
late347 said:
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.
Of course. If it works for real numbers, it has to work integers, which are a subset of the reals.
late347 said:
However in python I thiink it is not allowed to put fraction into float type of input?
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.
late347 said:
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)

How it's almost always done:
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)
 
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:
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")
 
You can say much more compressed in C:
Code:
#define MAX(a, b) ((a)>(b)) ? (a) : (b);
 
Last edited:
jedishrfu said:
Python:
if( (a - b) < tolerance):
    println("a equals b")
There's a typo above, I think - should be abs(a-b)<tolerance.
 
  • Like
Likes   Reactions: jedishrfu
Ibix said:
There's a typo above, I think - should be abs(a-b)<tolerance.

Yes, you're quite right!
 
jedishrfu said:
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:
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")
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.
 
  • Like
Likes   Reactions: jim mcnamara
Svein said:
You can say much more compressed in C:
Code:
#define MAX(a, b) ((a)>(b)) ? (a) : (b);

You can write the comparably concise
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:
Python:
print("Smallest number read was", min(map(float, input().split(' '))))
 
  • Like
Likes   Reactions: ChrisVer

Similar threads

Replies
55
Views
7K
  • · Replies 43 ·
2
Replies
43
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K