找到类型的最小允许值 [英] finding the minimum allowed value for a type

查看:57
本文介绍了找到类型的最小允许值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有标准方法可以找到数据类型的最小值?我是

按照std :: numeric_limits< T> :: min()的思路思考。但是那个

对浮点类型不起作用。我也不能使用

(-std :: numeric_limits< T> :: max()),因为这对于

无符号类型不起作用(并且在技术上不会对有符号的整数类型和

其他人工作,虽然它可能对他们来说足够好。


Do我需要实现一个使用

std :: numeric_limits<> :: min()的新模板,并将它专门用于浮点数/双打?

或者这已经是解决了图书馆的某个地方?


谢谢,

Adam H. Peterson

Is there a standard way to find the minimum value for a data type? I''m
thinking along the lines of std::numeric_limits<T>::min(). But that
doesn''t work for floating point types. I can''t use
(-std::numeric_limits<T>::max()) either, because that won''t work for
unsigned types (and technically won''t work for signed integer types and
others either, although it would probably be "good enough" for them).

Do I need to implement a new template that uses
std::numeric_limits<>::min() and just specialize it for floats/doubles?
Or is this already solved somewhere in the library?

Thanks,
Adam H. Peterson

推荐答案

" Adam H. Peterson" <啊** @ email.byu.edu>在消息中写道

新闻:bl *********** @ acs2.byu.edu ...
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.byu.edu...
是否有标准方法可以找到数据类型的最小值?我是按照std :: numeric_limits< T> :: min()的思路思考的。


这就是那个。 :-)

但是
对浮点类型不起作用。


当然可以。你尝试了什么?

我不能使用
(-std :: numeric_limits< T> :: max()),


你为什么要那样做?

因为那对于无符号类型不起作用


嗯,它没有''尝试否定

一个无符号类型是非常有意义的。

(技术上不适用于有符号整数类型和
其他类型,


你试过什么不会工作?

虽然它可能对他们来说足够好。
<我是否需要实现一个使用
std :: numeric_limits<> :: min()的新模板,并将其专门用于浮点数/双打?


标准库已经为除指针之外的所有标量类型专门化了模板



或者这已经解决了库?
Is there a standard way to find the minimum value for a data type? I''m
thinking along the lines of std::numeric_limits<T>::min().
That''s the one. :-)
But that
doesn''t work for floating point types.
Sure it does. What have you tried?
I can''t use
(-std::numeric_limits<T>::max()) either,
Why would you want to do that?
because that won''t work for
unsigned types
Well, it doesn''t make much sense to try to negate
an unsigned type.
(and technically won''t work for signed integer types and
others either,
What have you tried that "wont'' work"?
although it would probably be "good enough" for them).

Do I need to implement a new template that uses
std::numeric_limits<>::min() and just specialize it for floats/doubles?
The standard library already specializes the template
for all scalar types except pointers.
Or is this already solved somewhere in the library?




它已经完成了。为什么不发布一个小的,可编辑的
程序来证明你遇到的麻烦?


-Mike



It''s already done. Why not post a small, compilable
program that demonstrates the trouble you''re having?

-Mike


" Adam H. Peterson"写道:
"Adam H. Peterson" wrote:

有没有一种标准的方法来找到数据类型的最小值?我是按照std :: numeric_limits< T> :: min()的思路思考的。但是那对于浮点类型并不起作用。


在C(limits.h)中是否有类似于DBL_MIN和FLT_MIN的东西?

我不能使用
(-std: :numeric_limits< T> :: max()),因为它不适用于
无符号类型


不是无符号类型的最小值零?

(技术上不会对有符号整数类型和
其他类型工作,尽管它们可能对它们来说足够好)。


有符号整数类型可以使用std :: numeric_limits< T> :: min(),因为你指的是


我是否需要实现一个使用
std :: numeric_limits<> :: min()的新模板,并将其专门用于浮点数/双打?
或者这已经在库中的某处解决了?

Is there a standard way to find the minimum value for a data type? I''m
thinking along the lines of std::numeric_limits<T>::min(). But that
doesn''t work for floating point types.
Is there nothing analogous to DBL_MIN and FLT_MIN in C (limits.h)?
I can''t use
(-std::numeric_limits<T>::max()) either, because that won''t work for
unsigned types
Isn''t the minimum value for unsigned types zero?
(and technically won''t work for signed integer types and
others either, although it would probably be "good enough" for them).
Signed integer types can use std::numeric_limits<T>::min() as you
pointed out.
Do I need to implement a new template that uses
std::numeric_limits<>::min() and just specialize it for floats/doubles?
Or is this already solved somewhere in the library?




/ david


-

安德烈,一个简单的农民,只有一件事他的脑海里沿着东墙悄悄地走了过来:''安德烈,蠕动......安德烈,蠕动......安德烈,蠕动。''

- 未知



/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: ''Andre, creep... Andre, creep... Andre, creep.''
-- unknown




" Mike Wahler" < MK ****** @ mkwahler.net>在消息新闻中写道:ua **************** @ newsread3.news.pas.earthli nk.net ...

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:ua****************@newsread3.news.pas.earthli nk.net...
" Adam H. Peterson" ; <啊** @ email.byu.edu>在消息中写道
新闻:bl *********** @ acs2.byu.edu ...
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:bl***********@acs2.byu.edu...
是否有标准的方法来查找a的最小值数据类型?我正在思考std :: numeric_limits< T> :: min()。
Is there a standard way to find the minimum value for a data type? I''m
thinking along the lines of std::numeric_limits<T>::min().



这就是那个。 : - )



That''s the one. :-)




不,不是。 numeric_limits< double> :: min()报告最小的positilve可表示数字。

他想要的是最小可表示的(数字)。


他想要什么是:


模板< typename T> T real_min(){

if(numeric_limits< T> :: is_integer)返回numeric_limits< T> :: min();

else return -numeric_limits< T>: :max();

}



No it''s not. numeric_limits<double>::min() reports the smallest positilve representable number.
What he wants is the mininum representable (numerically).

What he wants is:

template <typename T> T real_min() {
if(numeric_limits<T>::is_integer) return numeric_limits<T>::min();
else return -numeric_limits<T>::max();
}


这篇关于找到类型的最小允许值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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