473,804 Members | 3,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with a calculation

double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct
answer is 6699.
The 6700 result really throws the entire group of calculations off.

Any suggestions welcome.
Thanks Mike


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
10 1952
> double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct
answer is 6699.
The 6700 result really throws the entire group of calculations off.


Do you have details on the system that returns 6700 ? (Hardware, OS,
Compiler).

Doing the calculation in 32 bit floats would probably return 6700, while
using 64 bit or 80 bit would return 6699.

Niels Dybdahl
Jul 22 '05 #2
Michael G wrote:
double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct
answer is 6699.


Is it? I'd say the 'correct' answer would depend on the type
of the argument of this floor() function you're using.

As (2447297.0-122.1)/365.25 is 6699.999726... then *if* the
argument of floor() can't hold it with enough precision the
result will be 6700.0

HTH,
- J.

PS. It's not really a proper group to ask this.
Jul 22 '05 #3

"Niels Dybdahl" <nd*@fjern.dett eesko-graphics.com> wrote in message
news:40******** *************@d text02.news.tel e.dk...
double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct
answer is 6699.
The 6700 result really throws the entire group of calculations off.


Do you have details on the system that returns 6700 ? (Hardware, OS,
Compiler).

I did some more checking and the problem is in the substraction.

The same system returns varying results in different contexts.
The OS is XP Home Edition. The hardware is P4 in a Toshiba laptop. The
compiler is VC++ 6.0.
We use julian day numbers for calculating dates within a game. If I unit
test the class the floor function returns 6699. But when the class is in
game, the flooring function returns 6700.

Mike


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #4
"Michael G" <mi****@montana .com> wrote in message
news:40******** @corp.newsgroup s.com...

"Niels Dybdahl" <nd*@fjern.dett eesko-graphics.com> wrote in message
news:40******** *************@d text02.news.tel e.dk...
double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct answer is 6699.
The 6700 result really throws the entire group of calculations off.


Do you have details on the system that returns 6700 ? (Hardware, OS,
Compiler).

I did some more checking and the problem is in the substraction.

The same system returns varying results in different contexts.
The OS is XP Home Edition. The hardware is P4 in a Toshiba laptop. The
compiler is VC++ 6.0.
We use julian day numbers for calculating dates within a game. If I unit
test the class the floor function returns 6699. But when the class is in
game, the flooring function returns 6700.


Are you sure you are using the same compiler setting for the unit test and
in game? If you make a fully optimized build rounding of floating point
number may be different than in debug builds. In release builds the
compiler will try to keep the intermediate results in the 80-bit floating
point registers as long as possible. This means there is no intermediate
rounding to 64-bit or 32-bit precision. With debug builds the intermediate
results are always written to and read from memory, and at that point the
intermediate result will be rounded to 64-bit or 32-bit precision. You can
fix this in release builds at the expense of performance by enabling the
"Improve Float Consistency" (/Op) option. See also: http://tinyurl.com/9an0

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 22 '05 #5
On Tue, 27 Apr 2004 08:24:38 -0600, "Michael G" <mi****@montana .com>
wrote:

"Niels Dybdahl" <nd*@fjern.dett eesko-graphics.com> wrote in message
news:40******* **************@ dtext02.news.te le.dk...
> double A = floor((2447297. 0 - 122.1)/365.25);
> i get differing results. It varies between 6699 and 6700. The correct
> answer is 6699.
> The 6700 result really throws the entire group of calculations off.


Do you have details on the system that returns 6700 ? (Hardware, OS,
Compiler).

I did some more checking and the problem is in the substraction.

The same system returns varying results in different contexts.
The OS is XP Home Edition. The hardware is P4 in a Toshiba laptop. The
compiler is VC++ 6.0.
We use julian day numbers for calculating dates within a game. If I unit
test the class the floor function returns 6699. But when the class is in
game, the flooring function returns 6700.


Why are you using doubles for date calculations? They are inaccurate
by nature (although not as inaccurate usually as you seem to be
experiencing), and integer maths will surely do the job. If inaccurate
dates are ok, I don't see the problem.

Note that (2447297.0 - 122.1)/365.25 will likely produce a different
result than if the same calculation if performed on variables, but the
difference will be small, and you should code to expect small
inaccuracies in any doubles (use <limits> to get more quantitive
information about the inaccuracies).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6
"Niels Dybdahl" <nd*@fjern.dett eesko-graphics.com> wrote in message news:<40******* **************@ dtext02.news.te le.dk>...
double A = floor((2447297. 0 - 122.1)/365.25);
i get differing results. It varies between 6699 and 6700. The correct
answer is 6699.
The 6700 result really throws the entire group of calculations off.


Do you have details on the system that returns 6700 ? (Hardware, OS,
Compiler).

Doing the calculation in 32 bit floats would probably return 6700, while
using 64 bit or 80 bit would return 6699.


Yes, you can typically only be sure of 7-digits of precision with
32-bit floating point values.

In general, addition and subtraction of numbers that differ by several
orders of magnitude should be a flag to watch out for precision errors
like the one the OP is having. It is usually possible to formulate
the original expression in a way that makes the precision problem more
tractable .. for example, if the numbers in the expression above are
really literals, the OP could remove the trivial integer division from
the problem to give:

double A = 6700.0 - ceil(0.1/365.25);

which will yield 6699 on a wider range of architectures.

Of course the problem is not likely to be so simple, but general
techniques can often be devised if you can say some things about the
arguments in advance. For example,

double func(double arg1, double arg2, double divisor) {
// assume arg1, arg2 non-negative and divisor > 1
double tmp1=floor(arg1/divisor);
double tmp2=floor(arg2/divisor);
double residue=(arg1-tmp1*divisor) - (arg2-tmp2*divisor);
return tmp1 - tmp2 - (residue<0) ? 1 : 0;
}

will carry out the schematic calculation from the OP's example,
yielding results that are somewhat less prone to precision error. The
key point is that the values that are subtracted are guaranteed to be
closer in magnitude than the original arguments. I expect there is a
more efficient way to write it, but it was just a quick example ...
also, sometimes efficiency must ultimately be sacrificed for the sake
of precision.

Check out a book like "The Art of Scientific Computing" for more info.

HTH, Dave Moore
Jul 22 '05 #7

"Jacek Dziedzic" <ja************ *@janowo.net> wrote in message
news:c6******** **@korweta.task .gda.pl...
PS. It's not really a proper group to ask this.


My apologies.

Mike


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #8

"Peter van Merkerk" <me*****@deadsp am.com> wrote in message
news:c6******** ****@ID-133164.news.uni-berlin.de...
"Michael G" <mi****@montana .com> wrote in message
news:40******** @corp.newsgroup s.com...

"Niels Dybdahl" <nd*@fjern.dett eesko-graphics.com> wrote in message
news:40******** *************@d text02.news.tel e.dk...
fix this in release builds at the expense of performance by enabling the
"Improve Float Consistency" (/Op) option. See also: http://tinyurl.com/9an0


Didn't help.
Any other suggestions?

Thanks, Mike


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #9
"Michael G" <mi****@montana .com> wrote in message
news:40******** @corp.newsgroup s.com...
fix this in release builds at the expense of performance by enabling the "Improve Float Consistency" (/Op) option. See also:

http://tinyurl.com/9an0

Didn't help.
Any other suggestions?


If building with exactly the same compiler settings still produces
different results, I don't know. Unless there is something in the game code
that changes the rounding mode of the FPU (for example with the
non-standard functions _controlfp() or _control87()), I have no idea what
else can cause this. In the past I written very floating point intensive
programs (which were far more complex than your example), and by enabling
the "Improve Float Consistency" option I could eliminate the differences
between debug and release builds, and even (to my surprise) with the MatLab
version of the programs.

Because with the VC 6.0 compiler computes the number passed to the floor()
function during compile time in both debug and release builds, the only
thing that appears to behave differently is the floor() function.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 22 '05 #10

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

Similar topics

3
7000
by: muser | last post by:
The following error appears for the code I'm writing: error C2062: type 'int' unexpected C:\Program Files\Microsoft Visual Studio\MyProjects\Valid\Zenith124\Zenith.cpp(427) : error C2143: syntax error : missing ';' before '{' C:\Program Files\Microsoft Visual Studio\MyProjects\Valid\Zenith124\Zenith.cpp(427) : error C2447: missing function header (old-style formal list?)
6
1930
by: Marcin Kalicinski | last post by:
Hi all, I have a problem with 'volatile' use in C++. The function get_clocks() below tries to use a 16 bit hardware counter to count time. The counter overflows very often. But an interrupt is generated everytime the counter overflows, and function handler() counts these overflows in variable n_overflows. The problem is that during calculation of time value in get_clocks()
4
2809
by: Trevor Best | last post by:
I have a report that's fairly simple, page headers and footers, detail has a subreport in (can vary in length). The customer wanted a signature block for them, their client and 3rd party. This was no problem, couple of boxes and labels in the report footer. Now customer wants this signature block on the foot of page 1 (stoopid IMO as that implies people read page 1 where at the end implies they read past page 1 at least). Anyway to...
2
12210
by: Maor Mishkin | last post by:
I have a problem with divide operation, the example is: double a =10.0; double b =220.0; double divAns = a/b; the calculation true result is 0.04545454545454545 but for some resone the calculation is changing to get the result 0.0454545468091 if you know how can i get the system to calc the first calculation or if you know of an object like BigNumber in java that can make
2
1380
by: David | last post by:
Hi, The data I am trying to print on the web looks as follows: Date 1 Record 1 Record 2 Record 3
3
1693
by: questions? | last post by:
I have a problem involves under flow/over flow. ################################################### # include <stdio.h> # include <math.h> # include <stdlib.h> double rate; double t; double u;
5
1115
by: jupiter | last post by:
hi friends, I am anil. I have begining level experience of c++ so I need ur advice. I have a problem for all think tanks in the group. what i want is 1. Access a web page (html) say ebay's page for particular item (I need to access different pages for several items approx. 1500) 2. Save that page in comp memory or on disk for processing 3. I am not sure if I can process that html in c++ or need to convert to txt first
4
1917
meLady
by: meLady | last post by:
Hello, I have a form called feedback with a subform called feedback Options. I did some counting calculation in master form "feedback" at the bottom page. the calculation is about count the number of yes, no and null that are in the feedback Options. the problem is that when navigate to a new form the calculation of the previous form is still in and I tried many times to find a solution but I couldn't ...
3
3558
by: mattmao | last post by:
Okay, I was asked by a friend about the result of this limit: http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg Not n->zero but n-> + infinite I really know nothing about advanced math, so I wrote a C program to help me: (BTW, I guess the result would be ln2.) #include <stdio.h>
0
9708
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
10589
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...
1
10327
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
7625
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
5527
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...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
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.