473,383 Members | 1,997 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,383 software developers and data experts.

Problem with integers

I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.

#include <stdio.h>
int main(void)
{

double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;

printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();

//Output for 2.30 is always 2 Hours and 29 minutes

return 0;
}
Nov 14 '05 #1
12 1663
In article <4bCJd.29090$IV5.21532@attbi_s54>,
"Kelly Goode" <no****@none.com> wrote:
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.


2.30 is not two and thirty hundredths; it is a number close to that
because C uses binary floating point numbers and not decimal floating
point numbers. It may be slightly larger or slightly smaller. Check what
your calculations do if it is just a tiny bit less than two and thirty
hundredths.
Nov 14 '05 #2
"Kelly Goode" <no****@none.com> wrote in message
news:4bCJd.29090$IV5.21532@attbi_s54...
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.


http://www.eskimo.com/~scs/C-faq/top.html
See section 14

Also see
http://docs.sun.com/source/806-3568/ncg_goldberg.html

-Mike
Nov 14 '05 #3
In article <4bCJd.29090$IV5.21532@attbi_s54>
Kelly Goode <no****@none.com> wrote:
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30?
It is not a problem with integers, but rather with floating point.
See the comp.lang.c FAQ, section 14.
// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?


2.30 is actually about 2.2999999523. Subtracting 2 gives 0.2999etc.,
and multiplying by 100 gives 29.999etc., which is of course 29 when
converted to "int".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
"Kelly Goode" <no****@none.com> wrote in message
news:4bCJd.29090$IV5.21532@attbi_s54...
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.

#include <stdio.h>
int main(void)
{

double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;


what happens here is an implicit conversion:
minutes = (int)((2.3 - 2) * 100)

which actually is equivalent to:
minutes = (int)(30.0 + EPSILON)

where EPSILON is some (implementation dependant) value that may be positive,
negative or 0.
If it is negative than conversion to int above yields 29.
That is because conversion from float to integer type is defined as
truncation of fractional part.

So, to make it work write:
minutes = 0.5 + (elapsed_time - hours) * 100;

or alternatively, if you use c99 compilant compiler add:
#include <math.h>
and write:
minutes = round((elapsed_time - hours) * 100);
--
x-pander
Nov 14 '05 #5
In article <ct*********@news2.newsguy.com> I wrote:
2.30 is actually about 2.2999999523. ...


Minor correction: I used an IEEE float, instead of an IEEE double.
The latter gives a number around 2.2999999999999998224 as the value
closest to 2.30.

The rest of the article stands. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6
"Kelly Goode" <no****@none.com> writes:
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.

#include <stdio.h>
int main(void)
{

double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;

printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();

//Output for 2.30 is always 2 Hours and 29 minutes

return 0;
}


There is no getch() function in standard C; it's probably something
specific to your operating system. If you want to call it (for
whatever reason), you should include the header that declares it.

You're trying to store the value 2.30 in an object of type double.
This value cannot be represented exactly in a binary floating-point
type. In the statement

minutes = (elapsed_time - hours) * 100;

you're computing (2.30 - 2) * 100 as a double value, which yields an
approximation to 30.0, but apparently you're getting something
something like 29.9999999999. Assigning this value to minutes, which
is an int, gives you 29, discarding the 0.9999999999.

You're asking the user to enter a real number which you're going to
interpret as a pair of integers. Instead, try reading it as a pair of
integers. Let the user enter "2:30", and extract the values 2 and 30
from the input, not from a floating-point value.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
On Wed, 26 Jan 2005 01:15:44 GMT, "Kelly Goode" <no****@none.com>
wrote:
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.
This is a strange way to represent time but that is a different issue.

#include <stdio.h>
int main(void)
{

double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);
2.3 cannot be represented exactly in binary. On your system, the
closest approximation is apparently 2.2999.... Now work out the
arithmetic and see what you get.

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;

printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();

//Output for 2.30 is always 2 Hours and 29 minutes

return 0;
}


<<Remove the del for email>>
Nov 14 '05 #8
Kelly Goode wrote:
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.
The number of posts we get from people who don't understand floating
point arithmetic is almost beyond counting. That's why your question
was long ago answered in the FAQ, which you should have checked before
posting. Even more, it has been answered countless times in all those
countless threads by those clueless ones that preceded you. Try the
following code on your implementation and think about the result:

#include <stdio.h>
#include <float.h>

int main(void)
{

double elapsed_time = 2.30;
double dminutes;
int hours;
int minutes;
hours = elapsed_time;
dminutes = (elapsed_time - hours) * 100;
printf
("minutes with purposeful precision beyond true\n"
" significance: %.*g\n",
DBL_DIG + 4, dminutes);
minutes = dminutes + 0.5;
printf("\n2.30-> %d:%d\n", hours, minutes);
return 0;
}

minutes with purposeful precision beyond true
significance: 29.99999999999998224
2.30-> 2:30

[OP's code] #include <stdio.h>
int main(void)
{

double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;

printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();

//Output for 2.30 is always 2 Hours and 29 minutes

return 0;
}

Nov 14 '05 #9
Kelly Goode wrote:

I have a problem with some simple math operations resulting in the
wrong integer. Why am I getting the wrong integer back for 2.30?
I should get back 2 hours and 30 minutes but instead I get 2 hours
and 29 minutes.
As you should. You should fix your program instead - see below.
#include <stdio.h>
int main(void)
{
double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;
/* Here is your basic error */
minutes = ((elapsed_time - hours) + 0.5) * 100;

printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
illustrating why you should keep line length under 72 char. My
suggested format:

printf("\nYou entered an elapsed time of %d hours"
" and %d minutes.\n", hours, minutes);
getch();
What can this possibly do for you? Just run it in a command line
window. Don't fight the silly IDE.
//Output for 2.30 is always 2 Hours and 29 minutes
return 0;
}


Floating point arithmetic is not exact arithmetic. Assigning
29.9999999999999 to an int results in 29.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #10
On Wed, 26 Jan 2005 06:02:01 GMT, CBFalconer
<cb********@yahoo.com> wrote:
Kelly Goode wrote:

I have a problem with some simple math operations resulting in the
wrong integer. Why am I getting the wrong integer back for 2.30?
I should get back 2 hours and 30 minutes but instead I get 2 hours
and 29 minutes.


As you should. You should fix your program instead - see below.

#include <stdio.h>
int main(void)
{
double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;


/* Here is your basic error */
minutes = ((elapsed_time - hours) + 0.5) * 100;


Why would he want to add 50 to the minutes? I think you meant:

minutes = (elapsed_time - hours) * 100 + 0.5;

Chris C
Nov 14 '05 #11
You need to read and understand

ftp://ftp.quitt.net/outgoing/goldbergFollowup.pdf
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #12
Chris Croughton wrote:

On Wed, 26 Jan 2005 06:02:01 GMT, CBFalconer
<cb********@yahoo.com> wrote:
Kelly Goode wrote:

I have a problem with some simple math operations resulting in the
wrong integer. Why am I getting the wrong integer back for 2.30?
I should get back 2 hours and 30 minutes but instead I get 2 hours
and 29 minutes.


As you should. You should fix your program instead - see below.

#include <stdio.h>
int main(void)
{
double elapsed_time;
int hours;
int minutes;

printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);

//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;

// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;


/* Here is your basic error */
minutes = ((elapsed_time - hours) + 0.5) * 100;


Why would he want to add 50 to the minutes? I think you meant:

minutes = (elapsed_time - hours) * 100 + 0.5;


Yup - well caught. Naughty keyboard. A cow flew by. Dang skeeters.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #13

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

Similar topics

3
by: Funnyweb | last post by:
I have a database table, which has field that could contain a single integer or a list of comma separated integers. Is it possible to match each row of that field against an array of integers...
0
by: Michael Mosel | last post by:
Hello! I am using Crystal Reports 9 (under Win2000 with SP3/SP4 - no difference) and try to create some reports from vb6. Everything works fine..., unless I install the centura runtime...
11
by: Faheem Mitha | last post by:
Hi, I'm not sure what would be more appropriate, so I'm ccing it to both alt.comp.lang.learn.c-c++ and comp.lang.python, with followup to alt.comp.lang.learn.c-c++. While working with a...
1
by: Emilio | last post by:
(MS Access 2002) Hello, I'm working with some big Census (PUMS) files, and I run into a peculiar problem once the data field exceeds five integers. I'll explain every step, since I am doing it in...
4
by: david.monaghan | last post by:
I have a table with the following fields - Location, Manager, CostCentre, Month and Headcount. What I am trying to do is a monthly rolling average headcount by Location, Manager, CostCentre. To...
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
5
by: Bo Yang | last post by:
Hi , I have writen a python program to slove a problem described as below: (Forgive my poor English !) Put the 2^n 0 or 1 to form a ring , and we can select any continuous n ones from the...
13
by: sonjaa | last post by:
Hi I'm new to programming in python and I hope that this is the problem. I've created a cellular automata program in python with the numpy array extensions. After each cycle/iteration the...
5
by: annCooper | last post by:
Exchange Rates Source file: Exchange.cs Input file: Exchange.in Output file: Exchange.out Using money to pay for goods and services usually makes life easier, but sometimes people prefer to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.