Question about program options in if-clause

In summary: If I recall correctly, it's called Archimedean trichotomy.It's called the Archimedean principle, and it states that if two numbers are equal to each other, then one of the two must be a negative number.
  • #1
late347
301
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
  • #2
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)
 
  • #3
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")
 
  • #4
You can say much more compressed in C:
Code:
#define MAX(a, b) ((a)>(b)) ? (a) : (b);
 
Last edited:
  • #5
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 jedishrfu
  • #6
Ibix said:
There's a typo above, I think - should be abs(a-b)<tolerance.

Yes, you're quite right!
 
  • #7
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 jim mcnamara
  • #8
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 ChrisVer

What are program options in if-clauses?

Program options in if-clauses refer to the different choices or paths that a program can take based on certain conditions or criteria being met. These options are typically defined within the if-clause and can be used to control the flow of the program.

How do you write an if-clause in a program?

To write an if-clause in a program, you typically use the "if" keyword followed by a set of parentheses containing a condition or expression to be evaluated. This is then followed by a set of curly braces where you can define the code to be executed if the condition is met.

What is the purpose of using if-clauses in a program?

The purpose of using if-clauses in a program is to control the flow of the program based on certain conditions. This allows the program to make decisions and perform different actions depending on the input or other factors, making it more versatile and adaptable to different scenarios.

Can you have multiple program options within an if-clause?

Yes, you can have multiple program options within an if-clause by using additional keywords such as "else" or "else if". These allow you to define different conditions and corresponding code to be executed if the initial condition is not met.

How do you debug an if-clause in a program?

To debug an if-clause in a program, you can use techniques such as printing out the value of the condition or using a debugger to step through the code and see where it may be going wrong. You can also use conditional breakpoints to pause the program at a specific point and check the values of variables and conditions.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
895
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
313
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top