473,399 Members | 4,177 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,399 software developers and data experts.

Double comparison for range is not working correctly

Hello, I am trying to compare a double to be within various ranges, but the comparison is not working correctly. Rounding precision is not a concern, because if the value will match the boundaries so rarely, and it is rounding up to the higher range is acceptable. Below is the doe at issue:

if (kq == 2)
{
if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][0] = 1;
else if ((fundt[kq-1][ky-1][2-1] >= 450,000,000.00) && (fundt[kq-1][ky-1][2-1] < 525,000,000.00)) ktti[ky-1][0] = 2;
else if ((fundt[kq-1][ky-1][2-1] >= 525,000,000.00) && (fundt[kq-1][ky-1][2-1] < 650,000,000.00)) ktti[ky-1][0] = 3;
else if ((fundt[kq-1][ky-1][2-1] >= 650,000,000.00) && (fundt[kq-1][ky-1][2-1] < 750,000,000.00)) ktti[ky-1][0] = 4;
else if ((fundt[kq-1][ky-1][2-1] >= 750,000,000.00) && (fundt[kq-1][ky-1][2-1] < 850,000,000.00)) ktti[ky-1][0] = 5;
else ktti[ky-1][0] = 6;
printf("DEBUG9: The Quarter is %d, Trust Fund is %20.3f, the Tax Table is %d\n", kq, fundt[kq-1][ky-1][2-1], ktti[ky-1][0]);
}
else if (kq==4)
{
if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][1] = 1;
else if ((fundt[kq-1][ky-1][2-1] >= 450,000,000.00) && (fundt[kq-1][ky-1][2-1] < 525,000,000.00)) ktti[ky-1][1] = 2;
else if ((fundt[kq-1][ky-1][2-1] >= 525,000,000.00) && (fundt[kq-1][ky-1][2-1] < 650,000,000.00)) ktti[ky-1][1] = 3;
else if ((fundt[kq-1][ky-1][2-1] >= 650,000,000.00) && (fundt[kq-1][ky-1][2-1] < 750,000,000.00)) ktti[ky-1][1] = 4;
else if ((fundt[kq-1][ky-1][2-1] >= 750,000,000.00) && (fundt[kq-1][ky-1][2-1] < 850,000,000.00)) ktti[ky-1][1] = 5;
else ktti[ky-1][1] = 6;
printf("DEBUG10: The Quarter is %d, Trust Fund is %20.3f, the Tax Table is %d\n", kq, fundt[kq-1][ky-1][2-1], ktti[ky-1][1]);
}
else;

I know that a triple-dimension array looks complex, but I can guarantee that is a double. The literal constants I am using should automatically be doubles. The subscript of [2-1] looks odd, but this is code I was given to maintain, and that is how it is written elsewhere, so I kept it for consistency. The problem I am having is that when I run this code, all the data I input is less than the 450 million value, but the run falls through all the if and if-else conditions, and the else code is what is actually executed, i.e., I always get six as my result. I am running on a Sun Sparc, and the compiler used is SunStudio 12. I have tried using variables, with the values listed above assigned to each variable, but it does work either. When I use variables, if the input value is negative, the comparison for less than 450 million works, but any positive input values will through to the else and give me six. Thank you for any help you can give me with this.
Mar 17 '15 #1
1 1256
weaknessforcats
9,208 Expert Mod 8TB
Floating point comparisons fail when using the equality operators. Is 2.0000234 equal to 2? Well, no. But the compiler will convert 2 to a double and do the compare and based on the number of decimal places the numbers may well compare equal.

Read this: http://floating-point-gui.de/errors/comparison/
Mar 17 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Mr Doppino | last post by:
Hi people, I would like to submit a problem about double comparison. Here's the code: struct Cross_point { int cross_id; int x, y; int street_oriz, street_vert; }; ...
17
by: Jonathan Burd | last post by:
Greetings everyone, Reading about the int64_t types added by C'99, I decided to implement a 64-bit version of atoi for my own library. However, for the test number provided, it isn't doing what...
2
by: MJR | last post by:
I live in Arizona. We of course do not change for DST. All of the data that is inserted into our database is in Arizona time. My client wants me to display the Time Zone the record was inserted in...
5
by: tshad | last post by:
I have been working with setting my drop boxes to allow double clicking to select an item. It worked fine until I made some changes. I then stripped the page down to the bare essentials to find...
5
by: Joel Barsotti | last post by:
I'm trying to get sql cache dependency working correctly. I've got sql server 2005 for my back end. I've don the "ALTER DATABASE dbName SET broker_enable" command. I made sure the user in my...
7
by: marty.overdear | last post by:
I am opening a new window using window.open. I need the new window to open on top of the parent everytime. I cannot use onBlur b/c the user will need to click back to the parent for information. ...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
2
by: irish10945 | last post by:
I have been using this great dynamic page flip from 76 design but I've run into some problems maybe someone can help me out with. I have about 70 pages in the book. I do not want people to have to...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am using an ajax masked edit extender along with a text box and masked edit validator and calendarExtender. It was working correctly but now for some reason when the user selects the image...
0
by: Limay | last post by:
I have designed a WordPress site with an EStore plugin. Until a couple weeks ago, it was working great. Now when someone adds a product to the cart and hits the checkout button we get the following...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.