有没有一个函数来检索一个范围内可用的不同值的数量? [英] Is there a function to retrieve the number of available distinct values within a range?
问题描述
我在一个应用程序中使用双精度浮点变量。
我规范了一些值的范围。从(例如,我有很多范围) -48.0
到 48.0
到 0.0 到
1.0
,使用这个简单的函数:
<$ c $ (double nonNormalizedValue,double min,double max,double shape){
return pow((nonNormalizedValue - min)/(max - min),1.0 / shape);
$ b我想知道可用和不同值映射范围到另一个。
在C ++中是否有准备好的功能?我已经看过
numeric_limits
,但是我找不到任何有用的东西。
解决方案
在C ++中是否有一个随时可用的函数?
也许。如果没有,很容易形成一个函数来为每个
double
值分配一个序列号。
假设匹配endian&大小和典型的FP布局,如 double64 ,下面是有效
-INF
到INF
。//为每个double值返回一个序列号。
//数字顺序的double值将会有连续的(+1)序列号。
uint64_t double_sequence(double x){
uint64_t u64;
memcpy(& u64,& x,sizeof u64);
if(u64& 0x8000000000000000){
u64 ^ = 0x8000000000000000;
返回0x8000000000000000 - u64;
}
返回u64 + 0x8000000000000000;
lock $ $ $ $ b $ p $是否有一个函数来检索在一个范围内可用的不同值?
简单地减去序列号。 +1或-1取决于开放或封闭范围。 / p>
double_sequence(1.0) - double_sequence(0.0)+ 1 - > 0x3ff0000000000001
double_sequence(48.0) - double_sequence(-48.0)+ 1 - > 0x8090000000000001
注释:
请记住,在2的权力范围内,FP是对数分布的整体和线性的。
对于大约一半的FP,| x | < 1.0
。
在FP.0.0到1.0之间的数字在16.0到32.0之间。
有两倍于
[ - 48.0 ... 48.0]
与[0.0 ... 1.0] $ c $主要是由于负值。
I'm using double precision floating point variables within an application I'm making.
I normalize some ranges of values. Going from (for example; I've many ranges)
-48.0
to48.0
to0.0
to1.0
, using this simply function:double ToNormalizedParam(double nonNormalizedValue, double min, double max, double shape) { return pow((nonNormalizedValue - min) / (max - min), 1.0 / shape); }
I'd like to know the differences in available and distinct values mapping from a range to another.
Is there a ready-to-go function in C++? I've looked at
numeric_limits
, but I can't find anything useful.解决方案Is there a ready-to-go function in C++?
Perhaps. If not, it is easy enough to form a function to assigned a sequence number to each
double
value.Assuming matching FP/integer for endian & size and typical FP layout like double64, the below is valid
-INF
toINF
.// Return a sequence number for each `double` value. // Numerically sequential `double` values will have successive (+1) sequence numbers. uint64_t double_sequence(double x) { uint64_t u64; memcpy(&u64, &x, sizeof u64); if (u64 & 0x8000000000000000) { u64 ^= 0x8000000000000000; return 0x8000000000000000 - u64; } return u64 + 0x8000000000000000; }
Is there a function to retrieve the number of available distinct values within a range?
Simply subtract the sequence numbers. +1 or -1 depending on if an open or closed range.
double_sequence(1.0) - double_sequence(0.0) + 1 --> 0x3ff0000000000001 double_sequence(48.0) - double_sequence(-48.0) + 1 --> 0x8090000000000001
Notes:
Keep in mind that FP are logarithmically distributed overall and linear within powers of 2.
For about half of all FP,|x| < 1.0
.
There are as many FP numbers 0.5 to 1.0 as between 16.0 to 32.0.
There are over twice as manydouble
in the[-48.0 ... 48.0]
versus[0.0 ... 1.0]
range, primarily due to negative values.这篇关于有没有一个函数来检索一个范围内可用的不同值的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!