Base-4 arithmetic in MATLAB
조회 수: 7 (최근 30일)
이전 댓글 표시
Does code already exist for doing Base-4 arithmetic in MATLAB? I find myself needing to average several numbers that are in Base-4, and I am unsure of exactly how to go about doing this. I have seen things like "convert to Base-10, do the math, then convert back", but this really only works if the result of the average is an integer. The thought has occured to me to simply truncate the resulting average (round down), and then convert that back to Base-4, but that seems like I may be inadvertently producing an erroneous average.
I would like to take my Base-4 numbers, add them all up, divide by the number of numbers, and get a resulting Base-4 answer - if this is possible.
Thoughts, suggestions?
댓글 수: 2
James Tursa
2017년 7월 29일
Why does converting to IEEE double numbers, doing the math, and then converting back not work for you? The average value is the same underlying number, regardless of base and regardless of whether the result is an integer or not. Why do you seem to think otherwise? Can you give a counterexample where this doesn't give the answer you think it should?
John D'Errico
2017년 7월 29일
The average of two numbers is not dependent on what base they are represented in. Why does it matter if the result is not an integer?
채택된 답변
John D'Errico
2017년 7월 30일
편집: John D'Errico
2017년 7월 30일
The average of two numbers is not dependent on what base they are represented in. Why does it matter if the result is not an integer?
If you will only accept an integer result, note that ANY integer is representable in base 4 as well as base 10. If a number is not an integer in base 10, then it is also not an integer in base 4 either. So converting to base 10 to do the arithmetic, then converting back to base 4 must be sufficient. You can freely truncate the base 10 result to an integer, or round it as you choose, before the reconversion to your chosen base.
Of course, yes, you CAN do the computations in base 4. This is not that difficult. But it is also not even remotely necessary. Or you could compute a fractional result in base 4. Again, not that difficult.
But nothing at all stops you from doing the arithmetic in any base you choose. A number base is merely a representation of an integer. It does not change that number in any significant way. (Ok, if we are talking about fractions, then one can argue that 1/3, as represented in base3 is exactly representable, whereas it is not representable exactly in base 10, or base 2, for that matter. But you seem to be asking about integer representations.)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!