• 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

Swapping two float values with no temp varaible

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I recently read the trick about swapping two int values with no temp variable (the XOR trick), but I discovered that that does not work with floats. Is there a way to swap two float variables with no temp variable?

Regards,

Manny
 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

Here's my code (mostraMensagem = showMessage).



Sorry for somethings, but it worked.


java Swapping
N1 = 3.14
N2 = 9.98

Swapping.........
N1 = 9.98
N2 = 3.1400003



Hope that my code helped you.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a specific counterexample but I am willing to bet that for sufficiently extreme values of the two floats (close to the maximum possible float value, close to the minimum possible float value, or so far apart in range that N1 + N2 = N1 and N2 <> 0) that doesn't work.

Actually, now that I reread your post, even the example you provided didn't work!
[ January 24, 2008: Message edited by: Paul Clapham ]
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theoretically the following code will work for all numbers (the comments indicate the proof of correctness; A and B are the original values):

There is one small problem: floating point arithmetic will probably cause some small rounding errors when using with float and double.


Edit: this will probably work with one workaround, which gives you TWO temporary variables:

[ January 25, 2008: Message edited by: Rob Prime ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works without ever declaring any more variables:

Like Rob's last solution, it even handles values like Float.POSITIVE_INFINITY and Float.NaN, which Rob's original subtraction solution can't handle at all.

Still, this is a lot of work to do to avoid something as trivial as allocating an extra temp variable.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Still, this is a lot of work to do to avoid something as trivial as allocating an extra temp variable.



These kinda tricks are asked in interviews, no use in real project.
 
Andre Brito
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody read my post?
It worked!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We all read your post. It's a good technique, but as your output showed, and as Paul Clapham pointed out, it has roundoff errors. So now we are discussing ways to swap the exact values. Try swapping 0.0000001F and 1000000. Or swapping 1 and Float.POSITIVE_INFINITY.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ankur rathi:
These kinda tricks are asked in interviews, no use in real project.


I hope everybody knows enough to avoid companies that think that this sort of question is suitable for interviews.
 
reply
    Bookmark Topic Watch Topic
  • New Topic