473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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

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...
Aug 18 '08 #1
7 3756

MrIncognito <St*********@gmail.comwrote in message...
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?

--
Bob R
POVrookie
Aug 18 '08 #2
On 18 Aug, 18:17, MrIncognito <StudioJa...@gmail.comwrote:
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.
Aug 18 '08 #3
On Aug 18, 10:17 am, MrIncognito <StudioJa...@gmail.comwrote:
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
Aug 18 '08 #4
On 18 Aug., 19:17, MrIncognito <StudioJa...@gmail.comwrote:
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
Aug 18 '08 #5
On Aug 18, 12:13*pm, gw7...@aol.com wrote:
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.
Aug 18 '08 #6
gw****@aol.com wrote:
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.)
Aug 18 '08 #7
MrIncognito wrote:
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;
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Aug 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Gregory | last post by:
Hello, I'm trying to do the above in order to process an image and return the result to an html image control. It fails and my key suspects are either the variable that I'm passing in -...
1
by: Michael Brennan-White | last post by:
If I submit my for using a get action the resulting page loads . If I use a post action I get an error page saying "The page cannot be found". I am calling the originating page!!! This happens...
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
2
by: Sooha Park Lee | last post by:
I would like to compare two "double" numbers. To overcome round-off error, I am using the following strategy: rounding each number and try to convert it into interger by multiplying 10E6. One...
5
by: Tamir Khason | last post by:
Someone knows "Arrange All" windows calculation formula used in MDI?
0
by: Gawn | last post by:
Dear all, Greeding from Thailand. Need help for my news script. I am trying to display news which keywords match the current page keywords. I am using Dreamweaver 8 and PhpMyAdmin to manage MySQL....
0
by: tarlino | last post by:
Hi! I'm play with the sound generation (c++ and directX). Now I would like to manage same filter on my sample. But now I must to convert my audio buffer "BYTE" (from DirectX) to an arry of "short"...
3
by: sigkill9 | last post by:
I've been working on this script all weekend and am almost done with it accept for one last detail; implementing a "process another number?" prompt after a user input is processed. The script...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.