Doubles are giving me problems


Posted on 16th Feb 2014 07:03 pm by admin

Ok so, first of all i made a double = 0.05, but when running the debugger it shows up as 0.04999. I know this has something to do with how doubles store but its causing problems for me. Anyway, in the code I'm trying to get only the right hand side(rhs) of the double variable jdn's decimal point. So in the code example I'm only trying to get .05. But rhs isn't always going to be the same and can have up to 0.00005 <- that fifth one there(can't remember what its called - hundred thousandth?). I have constant numbers right now for the sake of testing this. Now the counter goes through starting at 0.1, to make sure everything after the decimal point is 0. It goes through fine until it gets to 0.0002 - 0.0001 and then rhs = 9.99998e-005.
1
2
3
Did you know?Explore Trending and Topic pages for more stories like this.
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
29
30
#include #include #include using namespace std; int main() { double jdn = 2454115.05; double lhs = floor(jdn); double rhs = jdn - lhs; double counter = 0.1; if( rhs != 0.0 ) while( (rhs != 0.000001) || (counter != 0.00001) ) { if( (rhs - counter) <= 0 ) { counter *= 0.1; //cout << "rhs = " << rhs << endl; } else { rhs -= counter; cout << "rhs = " << rhs << endl; } } cout << "ok" << endl; return 0; }
Results:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
rhs = 0.04 rhs = 0.03 rhs = 0.02 rhs = 0.01 rhs = 0.009 rhs = 0.008 rhs = 0.007 rhs = 0.006 rhs = 0.005 rhs = 0.004 rhs = 0.003 rhs = 0.002 rhs = 0.001 rhs = 0.0009 rhs = 0.0008 rhs = 0.0007 rhs = 0.0006 rhs = 0.0005 rhs = 0.0004 rhs = 0.0003 rhs = 0.0002 rhs = 9.99998e-005
No comments posted yet

Your Answer:

Login to answer
120 Like 6 Dislike
Previous forums Next forums
Other forums

subtract 1 from value entered in text field
Hi

How do I subtract 1 from the vaue entered into a text field?

Thanks

php include email form with attachment
I have had no luck with finding a email form all over the Internet for various reasons. Some insecur

Connect to database that isn't localhost
I am currently doing a small script for a company that doesn't have mySql support on there hosting.

my login script page is not working on remote computers
hi my login is working on my computer, but when i tried 2 computers from 2 different locations, they

Deleting Partners on the Customer Master.
Does SAP handle removing the Partner from Open Sales Orders when a Partner is deleted in the Custome

Code error with Index.php
Error: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/runevid/public_

the problem with str_replace
$str="hahahahahahahahahahahahahaha";
$nn=1;
$str=str_replace('ha','MyGod',$str,$nn);

Securing a user input - need some confirmation
Hello All,

I am in the process of recoding a large proportion of an e-commerce site, one of t

Is there a way to override built in php functions without APD?
I am trying to use the
rename_function()
override_function()
options that are built into

Help building / using array
First - this is a cross-posting, if this is wrong I apologise! What started out as a MySQL query que

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash