473,413 Members | 2,043 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,413 software developers and data experts.

slight rounding problem

JNY
Hello,

I've searched for a solution, but can't find one.
When adding two numbers I'm not getting the expected result:

int testInt = 3056;
float testMant = 0.9001;
float testTotal;

testTotal = testInt+testMant;

This is producing 3056.9, not 3056.9001 as I would like. Using
doubles doesn't seem to help. Can anyone else?
Nov 14 '05 #1
2 1499
JNY wrote:
Hello,

I've searched for a solution, but can't find one.
When adding two numbers I'm not getting the expected result:

int testInt = 3056;
float testMant = 0.9001;
float testTotal;

testTotal = testInt+testMant;

This is producing 3056.9, not 3056.9001 as I would like. Using
doubles doesn't seem to help. Can anyone else?


Three questions
a) Why do you think floats have infinite precision?
b) Why didn't you check the FAQ before posting?
c) Why didn't you follow the newsgroup before posting. In the last week
similar questions were posted by at least three other people clueless
about this, who also violated usenet ettiquette by posting without
checking the FAQ or following the newsgroup before posting.

See if you can explain this (recompile for the limits of your system):

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

int main(void)
{
int testInt = 3056;
float testMantf = 0.9001, testTotalf;
double testMant = 0.9001, testTotal;
long double testMantl = 0.9001L, testTotall;

testTotalf = testInt + testMantf;
testTotal = testInt + testMantf;
testTotall = testInt + testMantf;
printf("float addend:\n"
" %d (int) + %.*g (float) = %.*g (float)\n"
" %d (int) + %.*g (float) = %.*g (double)\n"
" %d (int) + %.*g (float) = %.*Lg (long double)\n\n",
testInt, FLT_DIG, testMantf, FLT_DIG, testTotalf,
testInt, FLT_DIG, testMantf, DBL_DIG, testTotal,
testInt, FLT_DIG, testMantf, LDBL_DIG, testTotall);

testTotalf = testInt + testMant;
testTotal = testInt + testMant;
testTotall = testInt + testMant;
printf("double addend:\n"
" %d (int) + %.*g (double) = %.*g (float)\n"
" %d (int) + %.*g (double) = %.*g (double)\n"
" %d (int) + %.*g (double) = %.*Lg (long double)\n\n",
testInt, DBL_DIG, testMant, FLT_DIG, testTotalf,
testInt, DBL_DIG, testMant, DBL_DIG, testTotal,
testInt, DBL_DIG, testMant, LDBL_DIG, testTotall);
testTotalf = testInt + testMantl;
testTotal = testInt + testMantl;
testTotall = testInt + testMantl;
printf("long double addend:\n"
" %d (int) + %.*Lg (long double) = %.*g (float)\n"
" %d (int) + %.*Lg (long double) = %.*g (double)\n"
" %d (int) + %.*Lg (long double) = %.*Lg (long double)\n\n",
testInt, LDBL_DIG, testMantl, FLT_DIG, testTotalf,
testInt, LDBL_DIG, testMantl, DBL_DIG, testTotal,
testInt, LDBL_DIG, testMantl, LDBL_DIG, testTotall);
return 0;
}
float addend:
3056 (int) + 0.9001 (float) = 3056.9 (float)
3056 (int) + 0.9001 (float) = 3056.90014648438 (double)
3056 (int) + 0.9001 (float) = 3056.900146484375 (long double)

double addend:
3056 (int) + 0.9001 (double) = 3056.9 (float)
3056 (int) + 0.9001 (double) = 3056.9001 (double)
3056 (int) + 0.9001 (double) = 3056.90009999999984 (long double)

long double addend:i
3056 (int) + 0.9001 (long double) = 3056.9 (float)
3056 (int) + 0.9001 (long double) = 3056.9001 (double)
3056 (int) + 0.9001 (long double) = 3056.9001 (long double)

Nov 14 '05 #2
JNY wrote:
Hello,

I've searched for a solution, but can't find one.
When adding two numbers I'm not getting the expected result:

int testInt = 3056;
float testMant = 0.9001;
float testTotal;

testTotal = testInt+testMant;

This is producing 3056.9, not 3056.9001 as I would like. Using
doubles doesn't seem to help. Can anyone else?


You need a little more C practice.

#include <stdio.h>
int main(void) {
int testInt = 3056;
float testMant = 0.9001;
float testTotal;
testTotal = testInt+testMant;
printf("testTotal is %.4f\n", testTotal);
return 0;
}

testTotal is 3056.9001 (on my system)

But also, you should be conscious of the fact that floats are binary
and your expected outputs are decimal and that conversions among
number systems are usually inexact.

And more practice is always good.

"Programming is like shaving. If you don't do it every day, you're a
bum."

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #3

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

Similar topics

3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
0
by: Rick Gabel | last post by:
Trying to use the decimal datatype with SQL Server2000 and C#.Net and having a rounding problem. I am using SqlCommand/SqlParameter process to retrieve and insert a record into the table. One...
5
by: Jason | last post by:
I am having a rounding problem in a value i am trying to display in VB.NET. If I have the following code: Dim x As Single = 2726.795 Dim y As Single = Math.Round(2726.795, 2) Dim s as String =...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
4
by: Altman | last post by:
I am having a rounding problem all over the place and am struggling to fix it. My problem is that I have an unit price and a quantity. When I multiply them together they come out to 3.705. I...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
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...

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.