替换'浮动'? [英] Substitute 'float'?
问题描述
是否可以创建一个自定义类''Float'',它会表现为
作为内置浮动类型的替代品?
正如在另一个帖子中所提到的,我曾经匆匆尝试过这个尝试
并且抓住一些浮点顽皮,但却被各种各样的b
地方所困扰。这样做有根本障碍吗?
谢谢,
GA
Should it be possible to create a custom class, ''Float'', which would behave
as a drop-in replacement for the builtin float type?
As mentioned in another thread, I once tried this in rather a hurry to try
and catch some floating point naughtiness, but was stumped in various
places. Is there a fundamental obstacle to doing this?
thanks,
G.A.
推荐答案
Glen Able写道:
Glen Able wrote:
是否可以创建一个自定义类''Float'',它可以作为内置浮点类型的替代品?
是的,基本上。
正如另一个帖子所提到的,我曾经匆匆试过这个尝试
并抓住一些浮点顽皮,但被困在各个地方。这样做是否存在根本障碍?
Should it be possible to create a custom class, ''Float'', which would behave
as a drop-in replacement for the builtin float type?
Yes, basically.
As mentioned in another thread, I once tried this in rather a hurry to try
and catch some floating point naughtiness, but was stumped in various
places. Is there a fundamental obstacle to doing this?
我不记得了。当使用类和内置类型时,有些人可能会声称传奇的
性能下降,但是
IMO并不总是如此。鉴于良好的编译器和实现
,你应该获得相同的性能。
V
Not that I can recall. Some would probably claim the legendary
performance decrease when using classes versus built-in types, but
IMO it''s not always true. Given a good compiler and implementation
you should get the same performance.
V
Glen Able写道:
Glen Able wrote:
是否可以创建一个自定义类''Float'',它可以作为内置浮点类型的直接替换?
正如在另一个帖子中所提到的,我曾经匆匆试过这个尝试
并抓住一些浮点顽皮,但被困在各个地方。这样做有一个根本障碍吗?
...
Should it be possible to create a custom class, ''Float'', which would behave
as a drop-in replacement for the builtin float type?
As mentioned in another thread, I once tried this in rather a hurry to try
and catch some floating point naughtiness, but was stumped in various
places. Is there a fundamental obstacle to doing this?
...
想到的一个基本障碍是可变功能。
将非POD类作为可变参数传递会产生未定义的行为。
否则,它似乎是可行的。
-
祝你好运,
Andrey Tarasevich
One fundamental obstacle that comes to mind is variadic functions.
Passing non-POD classes as variadic arguments results in udefined behavior.
Otherwise, it appears to be doable.
--
Best regards,
Andrey Tarasevich
Glen Able发布:
Glen Able posted:
是否可以创建一个自定义类,''Float'',它将作为内置浮点类型的直接替换?
如上所述另一个线程,我曾经相当急于尝试抓住一些浮点顽皮,但却被各种各样的地方难住了。这样做有根本障碍吗?
谢谢,
GA
Should it be possible to create a custom class, ''Float'', which would
behave as a drop-in replacement for the builtin float type?
As mentioned in another thread, I once tried this in rather a hurry to
try and catch some floating point naughtiness, but was stumped in
various places. Is there a fundamental obstacle to doing this?
thanks,
G.A.
我不明白,你是什么意思是直接替换?让我
想到:
typedef float Float;
你想拥有一个不能浮动的浮动包含某些值?东西
喜欢:
class SpecialFloat
{
private:
浮动数据;
public:
SpecialFloat& operator =(浮点输入)
{
if(输入!= 666)
{
data = input ;
}
}
运算符float const()
{
返回数据;
}
};
或者你可能正在寻找不同的东西...
-JKop
I don''t understand, what do you mean by "a drop-in replacment"? Made me
think of:
typedef float Float;
Do you want to have a float which cannot contain certain values? Something
like:
class SpecialFloat
{
private:
float data;
public:
SpecialFloat& operator=(float input)
{
if (input != 666)
{
data = input;
}
}
operator float const()
{
return data;
}
};
Or maybe you''re looking for something different altogether...
-JKop
这篇关于替换'浮动'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!