简体   繁体   English

c++ 中的圆形 long double 到 unsigned long long

[英]Round long double to unsigned long long in c++

Is there any built-in function in C++ to convert long double into unsigned long long ? C++ 中是否有任何内置的 function 将long double转换为unsigned long long

As per this answer , it looks like double is not sufficient to represent some unsigned long long values.根据这个答案,看起来double不足以表示一些unsigned long long值。 Correct me if long double is also not enough.如果long double也不够,请纠正我。

I also know that there's a function called, roundl that rounds a long double but returns a long double again.我也知道有一个称为roundl的 roundl 将long double舍入但又返回long double
Another function lround can only return long and works on a smaller range of values.另一个 function lround只能返回long并且适用于较小范围的值。

So given a long double , how to do I round it to the nearest integer of type, unsigned long long ?所以给定一个long double ,如何将它四舍五入到最接近的 integer 类型, unsigned long long
Thanks.谢谢。

Related Question相关问题
Cast from unsigned long long to double and vice versa changes the value从 unsigned long long 转换为 double ,反之亦然会改变值

TL;DR use std::llroundl , assuming the initial value is smaller than 2^64 . TL;DR使用std::llroundl ,假设初始值小于2^64 If it is bigger, then it wouldn't fit anyway, so up to you to decide what to do with it.如果它更大,那么它无论如何都不适合,所以由你决定如何处理它。

IEEE 754 floating point numbers can represent any integer up to a certain point. IEEE 754 浮点数可以表示任何 integer 直到某个点。 If you convert an integer to such a number, and then back to the same integral type, you'll get the exact same value back as long as the integer is not larger than the representable limit.如果您将 integer 转换为这样的数字,然后返回相同的整数类型,只要 integer 不大于可表示的限制,您将得到完全相同的值。

For double precision numbers, as explained in this answer , that's 2^53 , which is big enough for all 32 bit integers.对于双精度数,如本答案中所述,即2^53 ,对于所有 32 位整数来说足够大。 We can apply the same general formula for for quad precision numbers, and get 2^113 , which is big enough for all 64 bit integers.我们可以对四精度数应用相同的通用公式,并得到2^113 ,这对于所有 64 位整数来说已经足够大了。

So, if long double on your implementation is a quad precision number, and unsigned long long is a 64 bit integer, then you can safely do the roundtrip:因此,如果您的实现中的long double是四精度数,并且unsigned long long是 64 位 integer,那么您可以安全地进行往返:

unsigned long long i = /* a very big value */;
long double d = i; // safe because it's a widening conversion
assert(static_cast<unsigned long long>(d) == i); // should pass

Now, if you don't have an integer in that long double , then of course there'll be some loss, because that fraction will have to be cut.现在,如果您在那个long double中没有 integer ,那么当然会有一些损失,因为必须削减该部分。 If you round the number first, you'll end up with an integer, and that is representable, so no loss (assuming it's not bigger than 2^113 ), but of course you'll still end up with a different number than the one you started with before the rounding.如果你先把数字四舍五入,你最终会得到一个 integer,这是可以表示的,所以没有损失(假设它不大于2^113 ),但当然你最终还是会得到一个不同的数字你在四舍五入之前开始的。

long double d = /* a very big value */;
unsigned long long i = static_cast<unsigned long long>(std::roundl(d));

Or you can avoid the cast altogether by using std::llroundl :或者您可以使用std::llroundl完全避免演员阵容:

long double d = /* a very big value */;
unsigned long long i = std::llroundl(d);

Although you'll need to be careful about negative numbers (but that's easy to check beforehand).尽管您需要小心负数(但这很容易事先检查)。

All of this assumes that your long double is never bigger than 2^64 , but if it were, then you wouldn't be able to fit it into the unsigned long long anyway.所有这些都假设您的long double永远不会大于2^64 ,但如果是这样,那么您无论如何都无法将它放入unsigned long long中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM