| CallMeCrazy (31) | |||
|
I am trying to write a program that will convert from one base to another and I am running into a problem with it and I have no idea what is happening. The problem is in convert(...), somehow, and I do not know how..., my second while loop is bugging on me. It would be much easier to have you see the ouput from my program than to explain what is happening. So, let us say the inputs to this function are: 10, 3, 556 What is wrong, is what is outputted after you press enter to enter 556. What newTotal should be is displayed above what it actually is. In my example, newTotal should be 70 but it is 69.
** I know that if I input a inputBase higher than outputBase or if inputBase > 9, it does not work. Leave that alone for now ^.^ Edit: The problem is at line 62. The value isn't what is should be... It would make a lot more sense if it was compiled. | |||
|
Last edited on
|
|||
| Chervil (1206) | |
| A line 309 characters long? You do know you're allowed to hit return occasionally, don't you. | |
|
|
|
| CallMeCrazy (31) | |
|
Yeah. That was just for basic debugging so I could tell what is going on. Just ouputs some variables and I prefer it on one line. The outputs and inputs in that while loop don't have to be there, they are just for debugging Edit: The problem is inside of that while loop | |
|
Last edited on
|
|
| theranga (100) | |
| I think you've got the maths wrong. You're ignoring inputBase completely. | |
|
|
|
| CallMeCrazy (31) | |||
Yeah. I know I am at the moment. I am just trying to get it to convert from base 10 to something else before I try and add conversion from other bases. My problem is in the three lines:
Here is an example. I enter a base 10 number and want to convert it to base 3. I enter 556 for my number. The second (I think...) time through the loop, the the first output statement displays newTotal - pow(outputBase, startingExponent) as 70. However, I then change the value in newTotal to newTotal - pow(outputBase, startingExponent). Then, on the next output, the value in newTotal is 69, instead of 70. | |||
|
Last edited on
|
|||
| CallMeCrazy (31) | |
| Bump | |
|
|
|
| toum (205) | ||||||||||
This is what I have when I compile and run your program:
Your problem could come from the fact you're silently converting floating point values in ints without rounding them beforehand. Depending on your system, pow(outputBase, startingExponent) might not give an exact integer. If the value you're expecting is Val, since you're using floating point types you could in fact get Val+err, with err a very small error. Computing
is in fact computing
ie
which will results in newTotal - Val - 1 instead of newTotal - Val. To avoid that, you should round the computed values or use an integer power function like this one:
| ||||||||||
|
Last edited on
|
||||||||||
| CallMeCrazy (31) | |||
That's just weird... When I run it I get:
It's just weird to me and int ^ int returns with a floating point error, but then again I don't know how Math.pow(...) handles the numbers. And, thanks for showing me another way to do it and stating what the problem actually was. | |||
|
|
|||
| toum (205) | |
|
Math.pow() requires to be called with floating point arguments. If you call it with int arguments, they are simply cast to floating point types. In general, it's best to avoid using floating point types when working with integer values, because you have to worry about that kind of stuff every line you write. By the way, did my suggestion correct the bug ? As I said, I was not able to reproduce the bug. So my explanation for it was just an educated guess. | |
|
Last edited on
|
|
| CallMeCrazy (31) | |
|
Yeah. Your solution fixed the bug I was having. But... If I called Math.pow() with two integers, let's say 5 and 6, why are they not cast them to 5.0f and 6.0f? | |
|
Last edited on
|
|
| toum (205) | |||||
|
I'm not sure I understand your question. When you write
what your compiler really does is
| |||||
|
Last edited on
|
|||||
| CallMeCrazy (31) | |||
Well, I'm just wondering how with:
The a does not get casted to 5.0 and the b 6.0, if they do they, then I'm just curious about where the error comes from. | |||
|
|
|||
| toum (205) | |||||||
|
They do get cast. The error comes from pow(). If you compute something like
your program must first compute c - pow(a,b). In the case you are mixing different types, the convention is to cast everybody to the most precise type. pow() returns a double and c is an int. Therefore c is cast to a double to perform the computation:
Since c is an int, the result is cast to an int in order to store it in c. So what your program really does is
It's in the computation of (double)c - pow(a,b) that the error can happen.
| |||||||
|
|
|||||||