Adding decimal numbers together?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Adding decimal numbers together?

 
0
  #1
Mar 19th, 2005
Hi ladies and gents,

I'm trying the next exercise:

Write a function value, wich adds the decimal numbers of 'n' together and return the result of this into main? For example:

0.1234 = 10
0.01234 = 10
0.3286 = 19

What Ive got so far is this:

  1. short amount (float n)
  2. {
  3. short a = 0, i, totvalue = 0;
  4.  
  5. for (i = 0; i < sizeof (n); i++)
  6. {
  7. a = n = (n * 10);
  8. n -= a;
  9. totvalue += a;
  10. }
  11.  
  12. return totvalue;
  13. }
  14.  
  15. int main()
  16. {
  17. double a = 0.1234;
  18.  
  19. cout<<"Value = " << amount(a) <<endl;
  20.  
  21. return 0;
  22. }

Problem is, when I run threw the loop in the function and arrive at the fourth digit '4', the compiler changes the number into '39997' and I get a wrong result :o

Also, is it possible to change the working of the loop using binary operators?

Thanks for the help :!:
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,126
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Adding decimal numbers together?

 
0
  #2
Mar 19th, 2005
I would convert the number to a string using sprintf() then loop through each character in the string and check with isdigit(). If true, then atoi() the character and add it to the sum. An interesting exercise!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Adding decimal numbers together?

 
0
  #3
Mar 19th, 2005
Thanks for the reply Vegaseat, but untill this part of exercises, there is no mentioning of using your examples in the piece of the book that Ive done yet, I'll have to go about 60 pages further before the explanation of strings is mentioned in detail so that I could try what you are suggesting

I'm at part 4 (classes and functions) :lol:
Strings is in part 6 ( Functions, strings en arrays)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 253
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Adding decimal numbers together?

 
0
  #4
Mar 19th, 2005
Originally Posted by JoBe
Problem is, when I run threw the loop in the function and arrive at the fourth digit '4', the compiler changes the number into '39997' and I get a wrong result :o
Floating point behaves this way because the value stored is an approximation. Since you're passing a double, you may want to receive it as a double rather than a float. Then you may discover...

Using sizeof (n) isn't the way to go about it either. That is the bytes required for the underlying storage, not the number of digits beyond the decimal place.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Adding decimal numbers together?

 
0
  #5
Mar 20th, 2005
Hi Dave,

Changing the variable from float into double doesn't change anything, so, your hint towards:
Then you may discover...
isn' found?

I understand what your saying about sizeof(), problem is, I don't have a clue as to how to change this towards the amount of numbers behind the 0,... ( don't know the name of ',' in English)

I think (almost certain) that I have to find a way to determine how many numbers are behind the ',' problem is, I don't know how I can do that
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Adding decimal numbers together?

 
0
  #6
Mar 20th, 2005
>don't know the name of ',' in English
Radix

>I don't know how I can do that
Floating-point is a bitch to work with. You would be better off converting to a string as vegaseat suggested:
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int precision (double val)
  8. {
  9. int n = 0;
  10.  
  11. ostringstream out;
  12. out<< val;
  13. string s = out.str();
  14.  
  15. return s.length() == 1 ? 0 : s.length() - 2;
  16. }
  17.  
  18. int main()
  19. {
  20. cout<< precision ( 0.0 ) <<endl;
  21. cout<< precision ( 0.1 ) <<endl;
  22. cout<< precision ( 0.12 ) <<endl;
  23. cout<< precision ( 0.123 ) <<endl;
  24. cout<< precision ( 0.1234 ) <<endl;
  25. cout<< precision ( 0.12345 ) <<endl;
  26. cout<< precision ( 0.123456 ) <<endl;
  27. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Adding decimal numbers together?

 
0
  #7
Mar 20th, 2005
Hi Narue,

Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up,
cout<< precision ( 0.123456 ) <<endl;
This for example shows as result 6 and not 21?

Also, why do you use int n = 0, it seems you don't use it in the function?

return s.length() == 1 ? 0 : s.length() - 2;
I understand this to be a selection, but I don't understand what it's doing?


I added this piece of code into the program that I wrote
if ( n >= 1){ a = n; n -= a; a = 0;}
This is because previously when I entered a number like 1,1234 it didn't get rid of the number before the radix.
Not that it matters since it seems I'm not going to solve this exercise with the code I wrote :-|


I see you, Dave and Vegaseat became moderators, very smart from Dani to do this, you guys are a real asset towards this C++ community, congrats to you all :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Adding decimal numbers together?

 
0
  #8
Mar 20th, 2005
>why is it that in your example, the decimal numbers aren't adding up
Because it's your project and not mine. I was just showing you the simplest way of determining how many digits of precision there are.

>why do you use int n = 0, it seems you don't use it in the function?
Leftovers from an older version.

>I understand this to be a selection, but I don't understand what it's doing?
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0. The equivalent is:
  1. if ( s.length() == 1 )
  2. return 0;
  3. else
  4. return s.length() - 2;
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Adding decimal numbers together?

 
0
  #9
Mar 21st, 2005
>Because it's your project and not mine. I was just showing you the simplest way of determining how many digits of precision there are.
Got it, thanks for the help Narue :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Adding decimal numbers together?

 
0
  #10
Mar 23rd, 2005
Hi Narue,

Ive been trying to get this exercise to work with the piece of code you gave me, but I'm stuck :!:

Ive put numbers 1, 2 & 3 left towards the code that I have trouble with
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. short amount (double val)
  9. {
  10. short a = 0, i, totvalue = 0;
  11. ostringstream out;
  12.  
  13. out<< val;
  14. string s = out.str();
  15.  
  16. for (i = 0; i < s.length(); ++i) // Number 1
  17. {
  18. a = s * 10; // Number 2
  19. s = s - a; // Number 3
  20. totvalue += a;
  21. }
  22.  
  23. return totvalue;
  24. }
  25.  
  26. int main()
  27. {
  28. double a = 0.999;
  29.  
  30. cout<<"Value = " << amount(a) <<endl;
  31.  
  32. return 0;
  33. }

1) I believe I can use this to determine the amount of digits behind the radix, correct?

2 & 3) when using this code, I get the following error C2676: binary '*' or '-': 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator

I understand it's not recognizing this operator, but, how can I get it to recognize it?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5850 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC