一个数字的标志...... [英] Sign of a number ...
问题描述
a简单的问题:
c ++中的哪个标准函数给我一个数字的符号?
< br $>
谢谢
Konrad
Hi,
a simple question:
Which standard function in c++ gives me the sign of a number?
Thanks
Konrad
推荐答案
KonradMühler写道:
Konrad Mühler wrote:
a简单的问题:
c ++中的哪个标准函数给我一个数字的符号?
a simple question:
Which standard function in c++ gives me the sign of a number?
C ++中没有标准函数(主要是因为没有
协议关于这样的函数将返回0,例如) 。
你为什么这么问?写得难吗?您是否担心
的性能?
V
-
请删除资金''回复电子邮件时的答案
我没有回复最热门的回复,请不要问
There is no standard function in C++ (mostly because there is no
agreement on what such function would return for 0, for example).
Why do you ask? Is it difficult to write? Are you concerned
with performance?
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Konrad Mühler写道:
Konrad Mühler wrote:
a简单问题:
哪个标准c ++中的函数给我一个数字的符号?
Hi,
a simple question:
Which standard function in c++ gives me the sign of a number?
除了Victor说的话......
那里'总是std :: less<>()。
In addition to what Victor said...
There''s always std::less<>().
11月14日下午4:14,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Nov 14, 4:14 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
KonradMühler写道:
Konrad Mühler wrote:
a simple问题:
a simple question:
c ++中的哪个标准函数给我一个数字的符号?
Which standard function in c++ gives me the sign of a number?
C ++中没有标准函数(主要是因为没有
协议关于这样的函数将为0返回的内容,例如) 。
你为什么这么问?写得难吗?您是否担心
的性能?
There is no standard function in C++ (mostly because there is no
agreement on what such function would return for 0, for example).
Why do you ask? Is it difficult to write? Are you concerned
with performance?
您可以使用位字段来完成:
#include< iostream>
模板< typename T>
struct Sign
{
bool S:1;
T剩余:( sizeof(T)-1);
};
int main()
{
Sign< intz;
int zero = 0;
int minusone = -1;
float minuszero = -0.0;
memcpy(& z,& zero,sizeof(zero));
std :: cout<< z.S<< std :: endl;
memcpy(& z,& minusone,sizeof(minusone));
std :: cout<< z.S<< std :: endl;
签收< intzz;
memcpy(& zz,& minuszero,sizeof(minuszero));
std :: cout<< z.S<< std :: endl;
返回0;
}
在哪种情况下0和-0不同,这不会使用''<''和/或''>兼容
a解决方案'
HTH
- Mathieu
You can do it with bits field:
#include <iostream>
template <typename T>
struct Sign
{
bool S:1;
T Remaining:(sizeof(T)-1);
};
int main()
{
Sign<intz;
int zero = 0;
int minusone = -1;
float minuszero = -0.0;
memcpy(&z,&zero,sizeof(zero));
std::cout << z.S << std::endl;
memcpy(&z,&minusone,sizeof(minusone));
std::cout << z.S << std::endl;
Sign<intzz;
memcpy(&zz,&minuszero,sizeof(minuszero));
std::cout << z.S << std::endl;
return 0;
}
In which case 0 and -0 are different, this will not be compatible with
a solution using ''<'' and/or ''>''
HTH
-Mathieu
这篇关于一个数字的标志......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!