Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

 
using namespace std;
void toBase (float n, float base)
{
string x = "0123456789ABCDEF";
if (n > 0)
{
toBase(n / base, base);
cout << x[n % base];
}
}
 

main() {
int a;
float num;
cout << "";
cin >> num;
cout << "";
cin >> a;
toBase(num, a);
 
}
 
I'm having trouble with this line of code - cout << x[n % base]; - i get an error message because i can't use % with float, and i'm having trouble implementing fmod due to x. How should that line of code look so my program can print out ten decimals correctly? I need a suitable substitute for % while using float. What function should i use?
Posted 29-Apr-13 9:14am
Edited 29-Apr-13 9:22am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can't use % with floats because modulus makes no sense when you aren't talking about integer values, which floats most definitely never are. Instead, try casting n as an integer (and do a comparison with the size of x to limit the range of values).
  Permalink  
Comments
jackson1892 at 29-Apr-13 14:31pm
   
That's kinda the problem, if i change base and n into integers, i don't get an error message, but the number is also without decimal fractions, so instead of getting (for example) 270F.FCAC083126 i get only 270F
 
I was hoping that there is a way of showing ten decimal fraction in the result without having to change the entire code, rather just replace int with float and use some other operator that performs the same task as %
OriginalGriff at 29-Apr-13 14:52pm
   
This is where is gets complicated.
"0.5" decimal in binary would be "0.1", and "0.25" is "0.01" and so forth, so it isn't the same calculation you need to make on each side of the decimal point. "2.25" in decimal would be "10.01" in binary and you get similar numbering problem when you move up the base. If you think about it, it makes sense, because "2.25" in decimal is "2 + 2/10 + 5/100" or "2 + 4/20 + 1/20" or "2 + 5/20" which brings us to "2 + 1/4"
 
If you want to play with bases on the right hand side of a decimal point, you have to work in the opposite direction from those on the left.
 
Is there any reason why you are trying to do this? I can't see any practical application off the top of my head...
jackson1892 at 29-Apr-13 15:01pm
   
It's a school assignment, i need to create a code that converts decimal number to any given base (from 2 to 16). So far i've been able to create an algorithm that shows the results without decimal fractions, which is why my code is incorrect. For example, if you type 7218.8192 in base 11 it should show 5472.90139A8535, but instead it shows only 5742 - it's the same with any given number. I have no idea what to do next, so any suggestion along with links to tutorials (as i'm pretty new at this) will be greatly appreciated.
OriginalGriff at 29-Apr-13 15:23pm
   
Are you sure you are supposed to be converting decimal numbers as in floating point, or decimal numbers as in base ten? Converting base ten integers is a pretty normal school task, but this is the first time I've heard of converting the "fractional" part as a school exercise. I guess this explains why you picked a recursive method where I would use a loop. :laugh:
 
To do it, you need to break the initial (LHS.RHS) value into two parts: one for the LHS and one for the RHS and handle them differently. The LHS you could handle in much the way you did above, but for the RHS multiply the "fractional" part by the base each time, instead of dividing it (sort of). Be aware that you are dealing with floating point numbers, so you are going to get some strange and unexpected effects - base ten values below 1 don't play nicely with binary storage, which is how all your numbers are stored in a computer: including float values.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The algorithm is broken:
You never output the delimiter between the whole number and fractional parts.
This algorithm could work for positive integers.
Regarding indexing into the string x, either cast n to int (or long) before the % or use fmod and cast the result.
  Permalink  
v3
Comments
jackson1892 at 29-Apr-13 14:42pm
   
I've never used fmod before, and string x is confusing me, if i use fmod without string x i don't get an error message, however the results are wrong and i don't know how to implement fmod correctly with string x. Any tips or maybe a link to a good novice tutorial?
Matt T Heffron at 29-Apr-13 14:52pm
   
As OriginalGriff says, this cannot do what you seem to be expecting.
In any case, you could use the fmod like:
cout << x[(int)fmod(n, base)];
ONLY for the whole number part, in which case casting n to int or long is better (and base must be an integer because a non-integral base would be done so differently it'd give you a *real* headache :)
Philippe Mori at 30-Apr-13 18:52pm
   
Even if fmod is used, that algorithm is sensible to truncation and rounding errors.
Matt T Heffron at 30-Apr-13 19:06pm
   
Of course, the whole approach is wrong for the stated problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 245
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100