帮助C ++中的小数到十进制代码 [英] Help with fraction to decimal code in C++

查看:68
本文介绍了帮助C ++中的小数到十进制代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


以下C ++代码是我在分数类中尝试将分数转换为小数的代码。 num和den都是整数。当我调用函数(f1.decimal_value())时,返回值为0.


任何人都可以帮助解决这个问题吗?




Hello,

The following C++ code is what I am trying to use in a fraction class to convert a fraction to a decimal. the num and den are both ints. When I make a call to the function (f1.decimal_value()), the return value is 0.

Can any one help with this problem?




展开 | 选择 | Wrap | 行号

推荐答案


您好,


以下C ++代码是我在分数类中尝试将分数转换为小数的代码。 num和den都是整数。当我调用函数(f1.decimal_value())时,返回值为0.


任何人都可以帮助解决这个问题吗?




Hello,

The following C++ code is what I am trying to use in a fraction class to convert a fraction to a decimal. the num and den are both ints. When I make a call to the function (f1.decimal_value()), the return value is 0.

Can any one help with this problem?




展开 | 选择 | Wrap | 行号



你不喜欢将num和den传递给函数,我猜它们被初始化为奇怪的东西 - 你是否尝试将它们打印出去看看它们在函数中有什么价值?


另一种选择是编译器可能试图优化你的鳕鱼e,看到num和den是整数,并且在不告诉你的情况下将decimal_num和decimal_den转换为整数 - 你可以尝试(如果上面的方法不起作用),例如:


double decimal_num = num * 1.0; //确保将double放入decimal_num
You don''t pass num and den to the function, I''m guessing they get initialized to something weird - did you try printing them out to see what values they had in the function?

Another option is that the compiler might be trying to ''optimize'' your code, seeing that num and den are ints, and converting decimal_num and decimal_den to ints without telling you - you could try (if the above doesn''t work) something like:

double decimal_num = num*1.0; // makes sure a double is put in decimal_num



我认为你是对的。打印后,num和den bo保持整数。我尝试使用* 1.0但不喜欢。还有另一种方法可以将num和den转换成double或者只是在函数中浮动吗?

提前谢谢

I think you were right. After printing num and den bo remain ints. I tried to use the *1.0 but no-joy. Is there another way to convert num and den to double or float just in that function?

Thanks in advance


他们正在正确传递/访问? (你没有说什么,所以我只是在检查。)
And they''re being passed/accessed correctly? (You didn''t say anything, so I''m just checking.)


这篇关于帮助C ++中的小数到十进制代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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