• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equality of ints

 
Ranch Hand
Posts: 218
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 218
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic