"ceil()" not working as expected...

M

MrIncognito

Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...
 
B

BobR

MrIncognito said:
Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...

1790 / 8 == long 223.
223 * 4 == 892.

That give you a clue?
 
G

gw7rib

Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...

I've had a similar problem. The problem is that, when it divides one
integer type by another, it wipes out any remainder. In your case, the
expression (header.width / 8) is, I presume, one integer type divided
by another, so because of the brackets round this it calculates this
and throws away the remainder *before* it tries to convert the answer
into a double. You need to convert either header.width or the 8 to a
double before the division. Such as:

linesize = ceil(((double) header.width) / 8) * 4;
or
linesize = ceil((double) header.width / 8) * 4;
or
linesize = ceil(header.width / 8.0) * 4;

Hope that helps.
Paul.
 
G

Greg Herlihy

Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...

Try:

long linesize = ceil( double(header.width) / 8) * 4;

In other words, the division operation should yield a double - instead
of an integer that is subsequently converted to a double.

Greg
 
P

peter koch

Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...

The problem is not with ceil but with your understanding of the
expression header.width / 8.

Presumably header.width is integral and if so the result will be
integral as well. I would solve it by changed the expression to
header.width / 8.0. This will make the expression double, letting you
avoid the ugly C-cast (don't ever use them!).

/Peter
 
M

MrIncognito

I've had a similar problem. The problem is that, when it divides one
integer type by another, it wipes out any remainder. In your case, the
expression (header.width / 8) is, I presume, one integer type divided
by another, so because of the brackets round this it calculates this
and throws away the remainder *before* it tries to convert the answer
into a double. You need to convert either header.width or the 8 to a
double before the division. Such as:

linesize = ceil(((double) header.width) / 8) * 4;
or
linesize = ceil((double) header.width / 8) * 4;
or
linesize = ceil(header.width / 8.0) * 4;

Hope that helps.
Paul.

OHHH
I never knew that would happen... I thought that dividing integers
throwing away the remainder only happened in other languages.

Thank you. Knowing this will help me a lot in the future.
 
J

Juha Nieminen

linesize = ceil(header.width / 8.0) * 4;

Btw, you can achieve the same result without resorting to floating
point numbers and the slow ceil() function by performing an "integer
ceil" of the division, like this:

linesize = ((header.width + 7) / 8) * 4;

(The trick is naturally doing the +7 before integer-dividing by 8.
This will effectively integer-divide by 8 rounding the result up.)
 
D

Daniel Pitts

MrIncognito said:
Am I using this correctly?
This line of code is supposed to find the number of bytes in one line
of a 16 color bitmap, but it doesn't seem to work most of the time.

long linesize = ceil((double) (header.width / 8)) * 4;

For example, when header.width is 1790, this code returns 892, when it
should be returning 896...

Just a hint, this form is generally faster, more straight-forward, and
doesn't require fp math:

long linesize = ((header.width + 7) / 8) * 4;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top