4

I was asked this on an interview. I was absolutely clueless how I to solve this...

/* You are writing a unit test to confirm the correctness of a function which
takes 3 float values as arguments. You decide to stress test it by testing
1000000 'random' inputs.
You find that the function will fail, but very rarely, so you include code
to print out all failure cases, hoping to grab a simple repro case which you
can debug into.
Note: All code here is run in a single-threaded environment. */

//...
// Some code sets a,b,c
//...
if ( testPasses(a,b,c) )
{
    printf("Pass\n");
}
else // someFunc fails, print the values so we can reproduce
{
    printf("Fail: %f %f %f\n", a, b, c);
}

Can you help me? Thanks!

Update: I am sorry guys I accidently deleted the question!!

here it is

// where testPasses has the following signature and executes deterministically 

// with no side effects:

    bool testPasses(float f1, float f2, float f3);

void testRepro()
{
    float a = // fill in from value printed by above code
    float b = // fill in from value printed by above code
    float c = // fill in from value printed by above code
    bool result = testPasses(a,b,c);
};

// Surprisingly, when you type in the 3 values printed out in testRepro()
// the test does not fail!
// Modify the original code and test bed to reproduce the problem reliably. 
Chebz
  • 1,375
  • 3
  • 14
  • 27
  • Does that code not work? – Jon Cram May 29 '11 at 20:40
  • 2
    I am absolutely clueless as to what is the actual question. Is the problem how to write the loop that injects the random numbers in? Or to be able to print the numbers? – Antti Huima May 29 '11 at 20:40
  • 1
    Is there something wrong with calling the code snippet 1000000 times in a `for` loop, while generating random numbers in each iteration? – In silico May 29 '11 at 20:40

2 Answers2

7

The problem is the precision used by printf on floating point numbers. They wanted to know if you could see that...

6502
  • 112,025
  • 15
  • 165
  • 265
  • ohhhhh now I see, damn this was such an easy question. I totally could have got it >.< Aghhhhhhhhhh! – Chebz May 29 '11 at 21:05
5

Print out the bytes representing the float instead of printing the value. Then you can plug in the exact value as a hex value.

char value[4] = { 0x12, 0x34, 0x56, 0x78 };
float *a = (float*)value;

// test using *a
pickypg
  • 22,034
  • 5
  • 72
  • 84
  • Pedantically, this isn't allowed. Use a `char value[4]` to avoid undefined behavior. – Ben Voigt May 29 '11 at 20:51
  • @Ben I changed it because it's not really code I'd hope to ever put into production either way, but I am curious, why is using a 32-bit integer any different than specifying it as a char[4] (other than giving more obvious control on byte ordering)? I'm sincerely curious because it specifically reminds me the [fast inverse square root](http://en.wikipedia.org/wiki/Fast_inverse_square_root) with the 32-bit int. – pickypg May 29 '11 at 21:08
  • 1
    Explained here: [What is the strict aliasing rule?](http://stackoverflow.com/q/98650/103167) – Ben Voigt May 29 '11 at 21:11