Optimizing Integer Comparison without If Statements

In summary, the conversation discusses a programming problem that asks for a program to determine the larger and smaller values of two user-inputted integers without using if statements. The answer provided uses a complex mathematical expression, causing confusion for the reader. The conversation also raises questions about the reliability and practicality of this method compared to using if statements.
  • #1
gerald
13
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
  • #2
sorry for the ambiguous title
 
  • #3
max(a,b)+min(a,b) = a+b
max(a,b)-min(a,b)= |a-b|


solve for an easier answer
 
  • #4
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.
 
  • #5
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:
  • #6
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.
 

What is the best way to start reading a programming book?

The best way to start reading a programming book is to first familiarize yourself with the basics of the programming language, such as syntax and common terminology. You can also start by reading the table of contents and skimming through the book to get an overview of the topics covered.

How can I make the most out of reading a programming book?

To make the most out of reading a programming book, it is important to actively engage with the material. This can include taking notes, practicing the code examples, and applying the concepts to your own projects. It is also helpful to set specific goals and a schedule for completing the book.

Is it necessary to read a programming book from cover to cover?

No, it is not necessary to read a programming book from cover to cover. Depending on your level of experience and the specific topics covered in the book, you may want to skip certain sections or chapters. It is important to focus on the areas that will be most beneficial to you.

How can I stay motivated while reading a programming book?

Staying motivated while reading a programming book can be challenging, especially if the material is complex or unfamiliar. To stay motivated, try breaking up your reading sessions into smaller chunks and taking breaks in between. You can also set mini-goals for each session and reward yourself for completing them.

Are there any additional resources I can use while reading a programming book?

Yes, there are many additional resources you can use while reading a programming book. These can include online tutorials, video courses, and coding challenges. It can also be helpful to join online communities or attend coding meetups to connect with others who are also learning the same programming language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • STEM Academic Advising
Replies
6
Views
847
Replies
40
Views
2K
Replies
2
Views
65
  • STEM Academic Advising
Replies
21
Views
2K
  • Linear and Abstract Algebra
Replies
7
Views
6K
  • STEM Academic Advising
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • STEM Academic Advising
Replies
2
Views
1K
  • Programming and Computer Science
Replies
17
Views
964
Back
Top