944,164 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7447
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 19th, 2005
0

Adding decimal numbers together?

Expand Post »
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 :!:
Similar Threads
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 19th, 2005
0

Re: Adding decimal numbers together?

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!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Mar 19th, 2005
0

Re: Adding decimal numbers together?

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)
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 19th, 2005
0

Re: Adding decimal numbers together?

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 20th, 2005
0

Re: Adding decimal numbers together?

Hi Dave,

Changing the variable from float into double doesn't change anything, so, your hint towards:
Quote ...
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
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 20th, 2005
0

Re: Adding decimal numbers together?

>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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 20th, 2005
0

Re: Adding decimal numbers together?

Hi Narue,

Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up,
Quote ...
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?

Quote ...
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
Quote ...
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 :!:
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 20th, 2005
0

Re: Adding decimal numbers together?

>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;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 21st, 2005
0

Re: Adding decimal numbers together?

>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 :!:
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 23rd, 2005
0

Re: Adding decimal numbers together?

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?
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Clear the Screen using WinApi functions
Next Thread in C Forum Timeline: Windows and Overlapped Serial Port Communications





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC