Optimizing Integer Comparison without If Statements

Click For Summary

Discussion Overview

The discussion revolves around finding a method to determine the larger and smaller values of two integers without using if statements, as posed in a programming problem. Participants explore various mathematical expressions and programming techniques related to this challenge.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a mathematical expression used to find the larger and smaller integers without if statements, questioning how it was derived.
  • Another participant suggests that the expressions provided in the programming book may be overly clever and impractical, especially when negative numbers are involved.
  • A different participant points out that the expressions might fail if floating-point numbers are used, leading to potential errors.
  • One participant clarifies that the problem specifically involves positive whole numbers, expressing appreciation for the trick despite its complexity.
  • Another participant argues that using if statements is computationally simpler and more reliable across all number types.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness and reliability of the mathematical expressions provided. There is no consensus on the best approach, and concerns about the applicability of the expressions in various contexts remain unresolved.

Contextual Notes

Participants note limitations regarding the handling of negative integers and floating-point numbers, as well as the specific requirement for positive whole numbers in the problem.

gerald
Messages
13
Reaction score
0
i'm reading a programming book and one of the problems it asks me to solve is to write a program that determines the larger and smaller values of two integers that the user inputs. I could not use if statements in the program.

I checked the answer and they had used this expression to come solve it.

long larger = (a*(a/b) + b*(b/a))/(a/b + b/a);
long smaller = (b*(a/b) + a*(b/a))/(a/b + b/a);

i was wondering how they have gone about finding those expressions? I'm not a math whiz so i was wondering if there was something in my mathematics education that was lacking.
 
Physics news on Phys.org
sorry for the ambiguous title
 
max(a,b)+min(a,b) = a+b
max(a,b)-min(a,b)= |a-b|


solve for an easier answer
 
gerald said:
i'm reading a programming book and one of the problems it asks me to solve is to write a program that determines the larger and smaller values of two integers that the user inputs. I could not use if statements in the program.

I checked the answer and they had used this expression to come solve it.

long larger = (a*(a/b) + b*(b/a))/(a/b + b/a);
long smaller = (b*(a/b) + a*(b/a))/(a/b + b/a);

i was wondering how they have gone about finding those expressions? I'm not a math whiz so i was wondering if there was something in my mathematics education that was lacking.

No there's nothing in your education that is lacking, it's just that writers of programming books have to show how clever they are by inventing silly tricks. This has several problems:

1) When I tried it with negative numbers it didn't work - it may do if you define division using negative integers in a different way, but not with the program I was using.

2) Suppose that you changed your integers to floating point, and your program had this code in. There would be no obvious reason why it needs changing - but suddenly people start finding mysterious errors when running the program.
 
hmm, i should have been more specific. it only wanted positive whole numbers and things I've learned in that chapter. this was a very neat trick.
 
Last edited:
An 'if' statement doesn't take as much computationally (and works with all number), so I'd never use that method if I were you.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
Replies
7
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
40
Views
5K
  • · Replies 15 ·
Replies
15
Views
3K