Discussion:
ceil function
(too old to reply)
BJ
2007-06-06 01:39:07 UTC
Permalink
WORD pcxHeader.XEnd = 229;
double tempBytePerLines;
short BytePerLines;

tempBytePerLines = (pcxHeader.XEnd / 8);
BytePerLines = ceil( tempBytePerLines);

BytePerLines is always equal to 28. But it should be 29, does
anyone know why?
Remy Lebeau (TeamB)
2007-06-06 03:21:32 UTC
Permalink
Post by BJ
BytePerLines is always equal to 28. But it should be 29,
does anyone know why?
Because it is supposed to. You are performing an integer division,
not a floating point division. The decimal point is lost before
tempBytePerLines is actually assigned. Since you are then passing a
whole number to ceil(), it returns the same number back.

In order to preserve the decimal point, you need to cast
pcxHeader.XEnd to a double when performing the division, ie:

tempBytePerLines = ((double)pcxHeader.XEnd / 8);


Gambit
Ed Mulroy
2007-06-06 03:25:07 UTC
Permalink
Post by BJ
BytePerLines is always equal to 28. But it should be 29, does
anyone know why?
According to the code you show BytePerLines should be 28, not 29.

WORD pcxHeader.XEnd = 229;
double tempBytePerLines;

tempBytePerLines = (pcxHeader.XEnd / 8);

pcxHeader.XEnd is an integer, an unsigned short.
8 is an integer.

pcxHeader.XEnd / 8 is the division of one integer by another. The answer is
an integer. The fractional part of the division is discarded during
integral division.

Then an assignment is made to tempBytePerLines. During the assignment the
integer is converted to a floating point double but the fractional part has
already been discarded.

Change
tempBytePerLines = (pcxHeader.XEnd / 8);
to
tempBytePerLines = (pcxHeader.XEnd / 8.0);

. Ed
Post by BJ
WORD pcxHeader.XEnd = 229;
double tempBytePerLines;
short BytePerLines;
tempBytePerLines = (pcxHeader.XEnd / 8);
BytePerLines = ceil( tempBytePerLines);
BytePerLines is always equal to 28. But it should be 29, does
anyone know why?
Loading...