This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# equality of ints

Ranch Hand
Posts: 127
5
So - I've read this:

Yes, I know this comes down to "divide by zero" - but: WHY? I thought I'm smart about maths (held record for 4 years not getting a single B but all A) - but what's the magic behind this abusing of Exception? Is there really any difference than "return a==b;"?

Marshal
Posts: 62883
203
This belongs in a different forum, and it merits a little something for its ingenuity.

Watch this space for my opinion

Campbell Ritchie
Marshal
Posts: 62883
203
Assuming you only get such an exception thrown when you are actually dividing by zero, what you are using is this logical fact:-
∀ₘ, ₙ • {m, n} ⊆ ℤ ⇒ ((m = n) ⇔ (m − n = 0))

That means, if you subtract two numbersintegers and get 0 as the difference, those two numbers are the same. Please search the Ranch for 0.9999999999.... being equal to 1.

You can probably prove the same for all numbers, real and imaginary.

Matt Wong
Ranch Hand
Posts: 127
5
Hm, I guess I have to express myself a bit more precise: Why would someone use such complicated stuff instead of just using a==b?
Also: My guess is, no matter how one would write such lines - I guess the compiler would simplyfi and optimized it anyway (not tested) and therfore it doesn't make sense in the first place to write such code as it would never run that way after JIT (wich most likely opitimize it down to some cpu instruction like storing both values in two registers, compares them and then does a conditional branch like "bz" (branch if zero) or "jez" (jump if equal zero)).

Even if this isn't java but some other language - but I guess any high-level language wich has something like "ArithmeticException" expressing a "divide by zero"-error has also a shortcut like a==b wich should result in the same result. I simple just can't come up with a reasonable explation for writing such mess. Maybe there's something more to it I just don't know about modern high-level languages.

Also: As the parameters are typed as ints - there is no such thing as to worry about floating-point im-precission as ints by definition - well, are integers - wich means a whole real number without fraction.

author
Posts: 23813
140

Campbell Ritchie wrote:
That means, if you subtract two numbersintegers and get 0 as the difference, those two numbers are the same. Please search the Ranch for 0.9999999999.... being equal to 1.

You can probably prove the same for all numbers, real and imaginary.

This trick only works for non-floating point numbers, so, the argument of floating point rounding errors do not apply here.

Henry

Master Rancher
Posts: 3080
108
I do remember having seen a topic about 0.9... being equal to 1, but I can't remember having seen a valid proof of it.

Anyway, it also doesn't work for languages that might throw an overflow error.

Ranch Hand
Posts: 463
As a math major I can confidently state that 2 + 2 = 5 for sufficiently large values of 2.

Math jokes are the best

Campbell Ritchie
Marshal
Posts: 62883
203
Also for sufficiently small values of 5. Of course, if you use languages like Forth, you can actually implement that sort of thing.

Campbell Ritchie
Marshal
Posts: 62883
203
When I said real and imaginary numbers, I meant the number as opposed to a binary representation of it.

Jim Venolia
Ranch Hand
Posts: 463
Man, that brought back a bad memory.  Years ago there was a language or implementation, don't remember which, where "2 = 3" was a valid statement.  It did what your nightmares think it did, it set "2" to the value 3.

Ranch Hand
Posts: 392
8

Matt Wong wrote:So - I've read this:

Yes, I know this comes down to "divide by zero" - but: WHY? I thought I'm smart about maths (held record for 4 years not getting a single B but all A) - but what's the magic behind this abusing of Exception? Is there really any difference than "return a==b;"?

World is beautiful because is so various
You wonder Why?  maybe because author will introduce exceptions? or another one imho elegant way(mess code is something way different) to do the same thing?
Programming is a full dynamic thing; something that you code today in a certain manner after some time you itself  will do it in a completely different way hopefully with more elegance; that's all about

 All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: RavenDB is an Open Source NoSQL Database thatâ€™s fully transactional (ACID) across your database https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database