需要帮助将小数部分转换为其他碱基的分数(2-16 [英] Need help converting decimal fractions to fractions from other bases (2-16

查看:70
本文介绍了需要帮助将小数部分转换为其他碱基的分数(2-16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够创建一个代码,将十进制数字(整数)转换为基数2到16的数字,但是我在分数部分遇到了很长一段时间的问题。

I''ve been able to create a code that converts decimal numbers (whole numbers) to base 2 to 16 numbers, but i''m having trouble with the fractional part for quite a while.

#include <iostream>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <cmath>

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

int main()
{

    float num;
    int a;
    float fNum = 12.34;
    int iPart = (int)fNum;
    float fPart = fNum - (float)iPart;
    cout << "";
    cin >> num;
    cout << "";
    cin >> a;
    toBase(num, a);
    cout << ".";
    toBase(fNum, a);
    cout << endl;
 

    }





这些是结果(10.5和2是十进制数字,基地,第二个数字是我应该得到的,第三个数字是我得到的):



10.5 - 1010.1 - 1010.1100

2



3.827 - 10.2110222122 - 10.110

3



82.7593 - 101.6744681322 - 101.13

9



7218.8192 - 5472.90139A8535 - 5472.11

11



8273.8 - 26B8.C - 26B8.C

15



23517.75 - 5BDD.C - 5BDD。 C $ /
16



92.33271 - 1011100.0101010100 - 1011100.1100

2



4095.839 - 7777.6554426416 - 7777.14

8



29672.9311 - 13033220.3232113021 - 13033220.30

4



9999.987 - 270F.FCAC083126 - 270F.C

16



十个测试结果中只有两个是正确的。我有几个小时的截止日期,所以我该怎么办,我应该添加或改变什么?



And these are the results (10.5 and 2 are the decimal number and the base, the second number is what i should get, the third number is what i get):

10.5 - 1010.1 - 1010.1100
2

3.827 - 10.2110222122 - 10.110
3

82.7593 - 101.6744681322 - 101.13
9

7218.8192 - 5472.90139A8535 - 5472.11
11

8273.8 - 26B8.C - 26B8.C
15

23517.75 - 5BDD.C - 5BDD.C
16

92.33271 - 1011100.0101010100 - 1011100.1100
2

4095.839 - 7777.6554426416 - 7777.14
8

29672.9311 - 13033220.3232113021 - 13033220.30
4

9999.987 - 270F.FCAC083126 - 270F.C
16

Only two of the ten test results are correct. I have a deadline in a few hours for the assignment, so how should i proceed, what should i add or change?

推荐答案

家庭作业我明白了...你是什么我想做的是得到小数部分(在。的右边)并继续乘以2并查看整数结果。
Homework I see... what you want to do is get the fractional portion (to the right of the ".") and keep multiplying by 2 and looking at the integer result.


你应该使用调试器和笔和纸张。



在纸上执行每一步并跟踪相应的代码。一旦结果不同,你就会知道使用的是什么以及你得到了什么。



你的代码的第一个问题是你总是用12.34代替fNum ...因此小数部分在基数a中总是12。

You should uses a debugger and also a pen and a paper.

Do each step on the paper and trace the corresponding code. As soon as the result differ, you will have an idea of what was used and what you get.

The first problem with your code is that you always uses 12.34 for fNum... thus the fractional part is always 12 in base a.
For 10.5 and 2, you get: 12 = 1x8 + 1x4 + 0x2 + 0x1 = 1100




For 3.828 and 3, you get: 12 = 1x9 + 1x3 + 1x0 = 110




For 82.7593 and 9, you get: 12 = 1x9 + 3%9 = 13




For 7218.8192 and 11, you get: 11 = 1x11 + 1%11 = 11




For 8273.8 and 15, you get: 12 = 12%15 = C





正如你在其他重复问题中提到的,处理这种情况的一种方法是乘以小数part ...





As mentionned in your other duplicate question, one way to handle that would be to multiply the fractional part...

For 10.5 and 2, you get for the fractional part:
0.5 * 2 = 1 ; 1 % 2 => 1 ; no more fractional part







For 3.827 and 3, you get for the fractional part:
0.827 * 3 = 2.481 ; 2 % 3 => 2
0.481 * 3 = 1.443 ; 1 % 3 => 1
0.443 * 3 = 1.329 ; 1 % 3 => 1
0.329 * 3 = 0.987 ; 0 % 3 => 0
0.987 * 3 = 2.961 ; 2 % 3 => 2
0.961 * 3 = 2.883 ; 2 % 3 => 2
0.883 * 3 = 2.649 ; 2 % 3 => 2 
...
In that case, you would stop when you have desired precision...


我只有两个建议为了你。查看对数规则,并使用连续分数。
I only have two suggestions for you. Look at logarithm rules, and use continued fractions.


这篇关于需要帮助将小数部分转换为其他碱基的分数(2-16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆