Click here to Skip to main content
15,903,012 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.

 
JokeRe: A Coding Horror from a subcontracted project... Pin
CPallini4-Jul-08 7:16
mveCPallini4-Jul-08 7:16 
GeneralRe: A Coding Horror from a subcontracted project... [modified] Pin
Rob Grainger13-Jul-08 23:48
Rob Grainger13-Jul-08 23:48 
GeneralRe: A Coding Horror from a subcontracted project... Pin
darkelv4-Jul-08 1:47
darkelv4-Jul-08 1:47 
GeneralRe: A Coding Horror from a subcontracted project... Pin
CARPETBURNER4-Jul-08 10:19
CARPETBURNER4-Jul-08 10:19 
GeneralRe: A Coding Horror from a subcontracted project... Pin
darkelv4-Jul-08 17:18
darkelv4-Jul-08 17:18 
GeneralRe: A Coding Horror from a subcontracted project... Pin
Paul Conrad4-Jul-08 10:38
professionalPaul Conrad4-Jul-08 10:38 
GeneralRe: A Coding Horror from a subcontracted project... Pin
Bert delaVega12-Jul-08 17:49
Bert delaVega12-Jul-08 17:49 
GeneralConvert double to fixed point integer format - hard way versus easy way... PinPopular
geoffs3-Jul-08 5:34
geoffs3-Jul-08 5:34 
I was delving into code for other modifications and came upon some previous programmer's apparent confusion...

Supposing you had a value in double (c++) format and wanted to convert it to a 20.12 fixed point format contained in a 32-bit value. The source value was guaranteed to also not be larger than what the 20 bit integer portion could represent. How would you go about implementing this conversion? Think about it. Meanwhile, here's the code I found (suitably cleansed of any incriminating information):

uint32 ConvertDoubleToFixedPoint32_12(double temp)
{
  uint32 whole;
  double getFrac;
  uint32 fraction;

  whole = (uint32)temp;

  // get the fractional part

  // put the whole number in a double
  getFrac = (double) whole;

  // take the whole from the original to get the fraction
  //4096 12-bits shift for the decimal
  fraction = (uint32)((temp - getFrac) * 4096);

  //shift up whole number to for the format, make room for the faction
  whole = whole << 12;
  whole |= fraction;

  return whole;
}


Got that? WTF | :WTF:

How would you have done that same conversion? Scroll down to see an alternative...


























static inline uint32 ConvertDoubleToFixedPoint32_12(double temp)
{
  return (static_cast<uint32>(temp * 4096));
}


Sigh...
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
PIEBALDconsult3-Jul-08 6:11
mvePIEBALDconsult3-Jul-08 6:11 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
geoffs3-Jul-08 6:16
geoffs3-Jul-08 6:16 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
Graham Bradshaw3-Jul-08 7:14
Graham Bradshaw3-Jul-08 7:14 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
geoffs3-Jul-08 7:32
geoffs3-Jul-08 7:32 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
PIEBALDconsult3-Jul-08 12:40
mvePIEBALDconsult3-Jul-08 12:40 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
geoffs3-Jul-08 15:48
geoffs3-Jul-08 15:48 
RantRe: Convert double to fixed point integer format - hard way versus easy way... Pin
killabyte6-Jul-08 13:16
killabyte6-Jul-08 13:16 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... PinPopular
Rob Grainger4-Jul-08 1:23
Rob Grainger4-Jul-08 1:23 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
PIEBALDconsult4-Jul-08 3:56
mvePIEBALDconsult4-Jul-08 3:56 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
DQNOK9-Jul-08 3:23
professionalDQNOK9-Jul-08 3:23 
GeneralRe: Convert double to fixed point integer format - hard way versus easy way... Pin
geoffs9-Jul-08 5:04
geoffs9-Jul-08 5:04 
GeneralDoes this qualify as horror? Pin
Megidolaon30-Jun-08 23:09
Megidolaon30-Jun-08 23:09 
GeneralRe: Does this qualify as horror? Pin
PIEBALDconsult1-Jul-08 4:50
mvePIEBALDconsult1-Jul-08 4:50 
GeneralRe: Does this qualify as horror? Pin
Megidolaon1-Jul-08 21:39
Megidolaon1-Jul-08 21:39 
GeneralRe: Does this qualify as horror? Pin
PIEBALDconsult2-Jul-08 12:48
mvePIEBALDconsult2-Jul-08 12:48 
GeneralRe: Does this qualify as horror? Pin
PIEBALDconsult2-Jul-08 12:52
mvePIEBALDconsult2-Jul-08 12:52 
JokeRe: Does this qualify as horror? PinPopular
Paul Conrad1-Jul-08 17:22
professionalPaul Conrad1-Jul-08 17:22 

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.