Re: Linux printf funny



Paul Burke wrote:
Stef wrote:

Is this a win32 console application or an old DOS application?


It was W32 console, compiled with VC++6.


It does not sound like a windows/linux problem, but more like a
programming error. So please show us the code.


#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "WinTypes.h"

If this is what I believe it is, you should find a way to remove it. I'm not sure it does any harm, but when trying to find arcane errors you'd better go with the safest and most standard setup.

#include "ftd2xx.h"
#include "ADC.h"

int TareWeight = 0; // ADC reading at no- load
double ScaleFactor = 1.0; // Bridge scale factor

double ReadWeight(void)
{
char buff[100];
double fweight; // Float version of weight
int Weight; // Weight as returned by ADC

Weight= ADCConvert();

fweight= (double)(Weight - TareWeight) * ScaleFactor;

Would the C promotions get in the way somehow? It shouldn't, but I'm still
wondering about it.

sprintf( buff, "%08x - %08x -> %9.3f", Weight, TareWeight, fweight);
MessageBox( 0,buff,"Conversion Result",0);

Safest setup again here:
printf("Conversion result: %08x - %08x -> %9.3f", Weight, TareWeight, fweight);

return fweight;
}



MessageBox() is just a function that displays the strings supplied, and is in fact a replacement for the Windows message box as in the program's earlier incarnation as a windows GUI program before it got converted to console (don't ask!).

Here's the console output:

Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087c0 - 000087bf -> 1.000
Conversion Result
000087bf - 000087bf -> 0.000
Conversion Result
000087c0 - 000087bf -> 1.000
Conversion Result
000087bf - 000087bf -> nan
Conversion Result
000087c0 - 000087bf -> nan
Conversion Result
000087bf - 000087bf -> nan

"nan" then remains sticky until I restart the program. You'll notice that the int readings are much the same as when the output is correct!

I know I found some errors in old DOS (turbo C) test programs when I
ported them to new versions of compilers (windows or linux). These
programs worked normally using the old compiler but failed using a
newer one. All the errors where my own (beginners)fault: uninitialized
vars, wrong format specifiers, near/far pointer stuff etc.


I could believe that better if it didn't work for the first few readings.

Paul Burke

Nan is usually because there is an error such a the root of a negative
number, or infinity, or things like that. Which is pretty strange given your
calculations...

You could try things such as:
float ScaleFactor = 1.0;
To try to avoid needing more accuracy than what a double (fweight) can
represent.

Is ScaleFactor modified somehow somewhere else in the code?

And which compiler to which target are you using?

Regards,
D.
.



Relevant Pages