如何从cpp_dec_float_50转换为cpp_int?以及关于漂浮在一般分? [英] How to convert from cpp_dec_float_50 to cpp_int? And regarding floating points in general?

查看:185
本文介绍了如何从cpp_dec_float_50转换为cpp_int?以及关于漂浮在一般分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基本目的是减去两个应该是等价浮动。
想想看: -

My basic aim was to subtract two supposed to be equivalent float. Consider this:-

float x=1;
float a=x/30-x/40;    
float b=x/30;
b-=x/40;
std::cout<<a-b<<std::endl;

我应该得到为零。但我没有。相反,我得到了大约10极少数^( - 10)

I should have got zero. But I didn't. Instead I got a very small number of around 10^(-10).

现在,有两个选择: -

Now, there were two options:-

首先,我想用的,而不是分数理性EX pressions(即处理分子和分母),然后转换成最终的理性前pression成分数。我通过Boost.rational这样做。我用cpp_int的分子和分母存储,因为分子和分母可以得到巨大的。它工作正常。但问题是,程序,我正在走的是一条很多时间。我认为这是因为必须处理大量的整数。

First, I thought of using rational expressions instead of fractions (that is deal with numerators and denominators) and then convert the final rational expression into fraction. I did this via Boost.rational. I used cpp_int to store the numerators and denominators since the numerators and denominators could get huge. It works fine. But the problem is that program which I'm making is taking a lot of time. I think that is because of having to deal with huge integers.

其次,有人劝我尝试定点运算。我不是很擅长。所以,我不知道,如果定点算术要么给出正确的答案?我在想什么是这个 - >
假设我想我的减法的正确高达50 precision的结果。所以我得到50位数字小数点左边用10滴小数部分的适当功率乘以它,并将它转换成cpp_int。我这样做既彩车。然后在这些cpp_int执行减法。
两个问题: -
首先,我无法cpp_float_dec_50转换为cpp_int。升压不直接允许这种(有损)转换。
其次,我只是这种方法在所有的工作没有信心。

Secondly, I was advised to try fixed point arithmetic. I'm not very good at it. So, I'm not sure if fixed point arithmetic would give correct answer either? What I was thinking was this-> Suppose I want the result of my subtraction correct upto the 50th precision. So I get 50 digits to the left of decimal point by multiplying it with appropriate power of 10. Drop the fractional part and convert it into cpp_int. I do this with both the floats. And then perform subtraction on these cpp_int. Two problems:- First, I am unable to convert cpp_float_dec_50 to cpp_int. Boost does not allow this kind of (lossy) conversion directly. Second, I'm just not confident on this approach working at all.

所以,最后两个问题: -
如何转换为cpp_float_dec_50 cpp_int?
什么样的方法是,在这个问题至今psented上下文$ P $两个最好的减两个浮点数?

So, finally two questions:- How to convert cpp_float_dec_50 to cpp_int? What kind of approach would be the best two subtract two floats in the context presented so far in the question?

感谢。和对不起,如果任何这给人的印象是非常noobish或愚蠢的问题。
我仍然在学习。

Thanks. And sorry if any of this comes across as highly noobish or stupid question. I'm still learning.

推荐答案

您需要决定是否要一个确切的再presentation与否。如果10 ^ -10太多的错误,你可能想毕竟正确性。我可以说,定点重新presentation不优于内置类型本身;如果延长其precision和能力,你到达一个多precision库:)

you need to decide whether you want an exact representation or not. if 10^-10 is too much of an error, you might want exactness after all. i can say that fixed point representation is no better than the built-in types per se; if you extend its precision and capabilities, you arrive at a multiprecision library :).

我可以推荐提振::多precision ,并发现它非常简单,但因为它是一个升压模板lib中,它需要一些额外的努力追捕编译器错误。数转换只是工作,但当然没有'免费的午餐';如果用错了你的结果是一样容易搞乱内置的数字。

i can recommend boost::multiprecision, and have found it fairly straightforward, though because it is a boost template lib, it takes some extra effort to hunt down compiler errors. number conversions just work, though of course there is no 'free lunch'; your results are just as easy to mess up as built-in numbers if used wrong.

事情要记住:


  • 性能precision的功能。控制住!

  • 衡量一段时间内的precision。重复乘法,例如,可能会增加precision无界。消除不必要的转换在你的数学。

  • 许多功能(如三角函数,对数,指数,...)没有为有理数实现。你只能围绕着这样的数学步骤,仔细,你需要放弃,接受控制误差(用浮点型)之前。

  • 您可以根据其precision要求分开你的程序。例如,在2D渲染的时候,变换查看空间(-1..1),那么你可以截断值转换成浮点和漂亮的速度运行。

  • 的boost ::多precision有提高性能(虽然他们不会修复precision控制失误)许多后端

我用下面的定义为我提振MP类型。在我的情况,我发现转向前pression模板关要快。

i use the following definitions for my boost mp types. in my case, i found turning expression templates off to be faster.

namespace mp = boost::multiprecision;
typedef mp::number<mp::cpp_int_backend<>, mp::et_off>       int_mp;
typedef mp::number<mp::cpp_rational_backend, mp::et_off>    rational_mp;
typedef mp::number<mp::cpp_dec_float<0>, mp::et_off>        float_mp;  // 0 means 'unlimited'

这篇关于如何从cpp_dec_float_50转换为cpp_int?以及关于漂浮在一般分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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