Click here to Skip to main content
15,886,362 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: That's one way to do it Pin
John Wellbelove18-Aug-21 22:41
John Wellbelove18-Aug-21 22:41 
GeneralRe: That's one way to do it Pin
11917640 Member 23-Aug-21 22:46
11917640 Member 23-Aug-21 22:46 
GeneralRe: That's one way to do it Pin
GuyThiebaut23-Aug-21 23:23
professionalGuyThiebaut23-Aug-21 23:23 
GeneralRe: That's one way to do it Pin
Sander Rossel29-Aug-21 2:50
professionalSander Rossel29-Aug-21 2:50 
GeneralRe: That's one way to do it Pin
jschell5-Sep-21 9:26
jschell5-Sep-21 9:26 
GeneralRe: That's one way to do it Pin
Gaston Verelst5-Oct-21 23:00
Gaston Verelst5-Oct-21 23:00 
GeneralRe: That's one way to do it Pin
Super Lloyd14-Oct-21 15:12
Super Lloyd14-Oct-21 15:12 
GeneralQuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
raddevus28-Jul-21 9:59
mvaraddevus28-Jul-21 9:59 
Watched video Fast Inverse Square Root — A Quake III Algorithm - YouTube[^]

It's a long video about how Quake III dev created a fast inverse of sqrt for 3D transforms.
Yes, I watched the video but I don't understand it completely. Roll eyes | :rolleyes:

First the Source Code
Here's my source code I used to confirm this weird syntax which...
Makes C Think the Address Contains a Long Even Though It's a Float

This is not a cast of a float value to long -- which would truncate decimal portion.
This is a conversion of the float bits to an address which is a long.
The bits stay in tact. !!!! Cool | :cool: I hope this gets your attention.

Third Line Syntax Is WEIRD
Really look at the third line below.

C++
float fVal = 13.3892F;
printf("Float value ==> fVal :%f\n",fVal); 
long  lVal = * (long *) &fVal; 
printf("float as long ==> lVal: %ld\n", lVal);


That 3rd line takes the address of the float &fVal and what!?! multiplies it times a long pointer? or something?!? Confused | :confused:

The Point
But the point is, that after you do that, C compiler believes that you've converted the bits that represented the float value into a long. CRAZY!!!

WTF | :WTF: I've never seen that syntax before. WTF | :WTF:

Here's the output:
Float value ==> fVal :13.389200
float as long ==> lVal: 5391137322


Here's a snapshot of what the narrator in the video says. Really funny (read the caption in the snapshot explaining how/why C convert float bits to long).

https://i.stack.imgur.com/YHyR5.png[^]

PS - If you already knew this, you are a freaking C genius. Seriously.
Or, you've been completely tortured by C. Laugh | :laugh:

EDIT / UPDATE
Oh, the syntax is easier than I thought.
It casts the float address as a long* and then gets the value stored at (*) that address and stores it at the address of the long -- which is actually the original address of the float (but now converted to a long). WHAT?!

modified 28-Jul-21 16:11pm.

JokeRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
PIEBALDconsult28-Jul-21 12:06
mvePIEBALDconsult28-Jul-21 12:06 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
Peter_in_278028-Jul-21 12:51
professionalPeter_in_278028-Jul-21 12:51 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
11917640 Member 28-Jul-21 18:58
11917640 Member 28-Jul-21 18:58 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
raddevus30-Jul-21 4:54
mvaraddevus30-Jul-21 4:54 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
kapalmuks<329-Aug-21 11:46
kapalmuks<329-Aug-21 11:46 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
Richard Andrew x6410-Aug-21 10:51
professionalRichard Andrew x6410-Aug-21 10:51 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
Peter_in_278011-Aug-21 13:18
professionalPeter_in_278011-Aug-21 13:18 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
Stuart Dootson18-Aug-21 0:48
professionalStuart Dootson18-Aug-21 0:48 
GeneralRe: QuakeIII: Fast Inverse of Sqrt in C (float to long) Pin
raddevus19-Aug-21 9:15
mvaraddevus19-Aug-21 9:15 
GeneralYou expect me to use this with what, now? Pin
honey the codewitch19-Jun-21 7:46
mvahoney the codewitch19-Jun-21 7:46 
GeneralRe: You expect me to use this with what, now? Pin
Kenneth Haugland19-Jun-21 8:18
mvaKenneth Haugland19-Jun-21 8:18 
GeneralRe: You expect me to use this with what, now? Pin
markrlondon19-Jun-21 11:44
markrlondon19-Jun-21 11:44 
GeneralRe: You expect me to use this with what, now? Pin
honey the codewitch19-Jun-21 11:57
mvahoney the codewitch19-Jun-21 11:57 
GeneralRe: You expect me to use this with what, now? Pin
jschell7-Jul-21 8:06
jschell7-Jul-21 8:06 
GeneralRe: You expect me to use this with what, now? Pin
enhzflep15-Jul-21 23:11
enhzflep15-Jul-21 23:11 
GeneralRe: You expect me to use this with what, now? Pin
honey the codewitch16-Jul-21 4:03
mvahoney the codewitch16-Jul-21 4:03 
GeneralRe: You expect me to use this with what, now? Pin
enhzflep16-Jul-21 6:06
enhzflep16-Jul-21 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.