• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

equality of ints  RSS feed

 
Ranch Hand
Posts: 106
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

[edit]You can probably prove the same for all numbers, real and imaginary.
 
Matt Wong
Ranch Hand
Posts: 106
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

[edit]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: 3002
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 443
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I said real and imaginary numbers, I meant the number as opposed to a binary representation of it.
 
Jim Venolia
Ranch Hand
Posts: 443
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 355
8
BSD Debian Open BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!