473,785 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 3780

MrIncognito <St*********@gm ail.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...@gm ail.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.wid th / 8.0) * 4;

Hope that helps.
Paul.
Aug 18 '08 #3
On Aug 18, 10:17 am, MrIncognito <StudioJa...@gm ail.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.w idth) / 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...@gm ail.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.wid th / 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.wid th / 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
13087
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 - ImageName - for processing, or the return data which is done (or not as the case may be) by imagejpeg($img) in the script, after header("Content-type: image/jpeg"). Any insights would be most welcome, especially debugging techniques and a pointer to...
1
4196
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 in IE as well as FireFox. This code has been tested on a Win2003 server, IIS6, PHP 5.0.3, mySQL 4.1.8 and it works fine. The problem server is a Win2k server, IIS5, PHP 5.0.4, mySQL 4.1.11.
6
4061
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). // Integer calculation (fast) int iround(const double x) { return (x>=0 ? static_cast<int>(x + 0.5) : static_cast<int>(x + 0.5) } // Floating point calculation (slow) double dround(const double x) {
2
7420
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 example of number is 0.765543. I do not want "ceil" or "floor". Any idea will be welcome. Can I find any smart code already existed on the web?
5
1585
by: Tamir Khason | last post by:
Someone knows "Arrange All" windows calculation formula used in MDI?
0
1670
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. May I explain you what I have done. My database and data in that look like this. //* Data Base -- phpMyAdmin SQL Dump -- version 2.6.2-pl1 -- http://www.phpmyadmin.net --
0
1596
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" and viceversa. I found this source: ************************************************************** double dPowI = ceil( log10( (double)iLen / ( ((double)wBitsPerSample / 8 ) + wChannels - 1 ) ) / log10( 2.0 ) ); int iNearTwoPower = (int)pow(...
3
6299
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 works like this: First, the user enters a number to test (to find the two prime numbers that make up that number). Next the program finds the numbers, outputs them, then quits. What I want to do is create a prompt after the number has been processed...
3
4515
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 error-trapping (if it finds an error in one of the xml files, the script stops). Recently, the script has stopped working because one of the xml files is badly formed. So I decided to rewrite the script with better error trapping; the script should...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.