home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2690
Discussions: 188,384, Posts: 2,243,499, Members: 232,614
Old February 7th, 2003, 09:12 PM   Digg it!   #1 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
comparing the double result of and terminating a loop

I'm running a loop where I sum very small numbers. I really think that I only need to be accurate to 35 digits or so. How do I compare only the first 35 digits of a double number to another number?

thanks
-Z

zskillz is offline   Reply With Quote
Old February 8th, 2003, 03:42 AM     #2 (permalink)
Member
 
Join Date: Sep 2002
Posts: 364
What do you mean by compare? See if they are the same or something?

Creosote is offline   Reply With Quote
Old February 8th, 2003, 12:26 PM     #3 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
double epsilon = 10^-35;

if (abs(a - b) < epsilon ) {
// they are the same to within +- epsilon
} else if ( a < b ) {
// a < (b - epsilon)
} else {
// a > b + epsilon
}

strangerstill is offline   Reply With Quote
Old February 8th, 2003, 02:29 PM     #4 (permalink)
Senior Member
 
Join Date: Oct 2001
Posts: 881
Send a message via AIM to zskillz
hmmm... i tried something along those lines stranger, but i need to know how big a double number really is before i pick an acceptable epsilon. I'm having trouble finding it.... apparently my google search technique needs some work.

-Z
zskillz is offline   Reply With Quote
Old February 8th, 2003, 03:06 PM     #5 (permalink)
Member
 
Join Date: Sep 2002
Posts: 364
There has to be a better way than this--but you could convert it to a string and count t the characters.
Creosote is offline   Reply With Quote
Old February 8th, 2003, 03:42 PM     #6 (permalink)
Ultimate Member
 
strangerstill's Avatar
 
Join Date: Oct 2001
Posts: 1,542
So... use

double epsilon = max(abs(a), abs(b)) * 10^-35;

if (abs(a - b) <= epsilon) {
...

Is that what you mean? Or do you need to know the precision of the double type?

Floating point numbers are standardised in IEEE 754 - try http://www.google.com/search?hl=en&l...=Google+Search

The libraries should contain constants for the least and greatest positive values that exist in each type.
strangerstill is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Most Active Discussions
Is It Just Me? (2896)
The United States Debt (20)
3-days in and no threads about Gaza (160)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
hp compaq nc6000 problems (138)
Folderchat Weekday thread (441)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (15)
Recent Discussions
ACPI controller halt on boot (2)
Building a gaming computer advi.. (3)
Worth the upgrade?? (15)
Folderchat Weekday thread (441)
ADVICE (0)
How to increase my ram? (5)
Help with an Ati Radeon HD 4850.. (27)
CPU wont boot (4)
2nd video card (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 10:00 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28