Click here to Skip to main content
15,917,320 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
atverweij20-Aug-21 1:32
atverweij20-Aug-21 1:32 
GeneralRe: That's one way to do it Pin
kapalmuks<329-Aug-21 11:41
kapalmuks<329-Aug-21 11:41 
GeneralRe: That's one way to do it PinPopular
MarkTJohnson18-Aug-21 3:23
professionalMarkTJohnson18-Aug-21 3:23 
GeneralRe: That's one way to do it Pin
wapiti6418-Aug-21 4:54
wapiti6418-Aug-21 4:54 
GeneralRe: That's one way to do it Pin
Daniel Pfeffer18-Aug-21 23:22
professionalDaniel Pfeffer18-Aug-21 23:22 
GeneralRe: That's one way to do it Pin
Jörgen Andersson24-Aug-21 7:53
professionalJörgen Andersson24-Aug-21 7:53 
GeneralRe: That's one way to do it Pin
kapalmuks<329-Aug-21 12:09
kapalmuks<329-Aug-21 12:09 
GeneralRe: That's one way to do it Pin
Kirk 1038982118-Aug-21 3:46
Kirk 1038982118-Aug-21 3:46 
GeneralRe: That's one way to do it Pin
John Wellbelove18-Aug-21 5:20
John Wellbelove18-Aug-21 5:20 
GeneralRe: That's one way to do it Pin
Richard Deeming18-Aug-21 6:32
mveRichard Deeming18-Aug-21 6:32 
GeneralRe: That's one way to do it Pin
Nelek19-Aug-21 0:55
protectorNelek19-Aug-21 0:55 
GeneralRe: That's one way to do it Pin
Richard Deeming19-Aug-21 0:58
mveRichard Deeming19-Aug-21 0:58 
GeneralRe: That's one way to do it Pin
kapalmuks<329-Aug-21 12:01
kapalmuks<329-Aug-21 12:01 
GeneralRe: That's one way to do it Pin
MarkTJohnson18-Aug-21 13:29
professionalMarkTJohnson18-Aug-21 13:29 
GeneralRe: That's one way to do it Pin
Richard Deeming18-Aug-21 22:08
mveRichard Deeming18-Aug-21 22:08 
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 

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.