Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Substitute 'float'?

Reply
Thread Tools

Substitute 'float'?

 
 
Glen Able
Guest
Posts: n/a
 
      07-08-2004
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.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-08-2004
Glen Able wrote:
> 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?


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
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      07-08-2004
Glen Able wrote:
> 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?
> ...


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

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-08-2004
Glen Able posted:

> 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.


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
 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      07-08-2004
Andrey Tarasevich wrote:
> One fundamental obstacle that comes to mind is variadic functions.
> Passing non-POD classes as variadic arguments results in udefined behavior.


I was not aware of that. Not that I write many variadic functions...

Good to know, nonetheless.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-08-2004
Julie wrote:

> Andrey Tarasevich wrote:
>> One fundamental obstacle that comes to mind is variadic functions.
>> Passing non-POD classes as variadic arguments results in udefined
>> behavior.

>
> I was not aware of that. Not that I write many variadic functions...


No, but you might have the idea to pass one of your objects to printf
one day. Another thing that comes to mind is conversions. The rules for
built-in types are different from the rules for user defined types.

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-08-2004
Glen Able wrote:

> Should it be possible to create a custom class, 'Float',
> Is this a valid as a drop-in replacement for the builtin float type?


I have done this many times.

#include "Float.h"
#define float Float

> 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.


Can you show us an example?

> Is there a fundamental obstacle to doing this?


No.

The abstraction used in the C++ computer programming language
to define a portable "built-in" type float virtually guarantees
that you can substitute your own definition of float as long as
you implement the abstract data type defined by the standard.
 
Reply With Quote
 
Glen Able
Guest
Posts: n/a
 
      07-09-2004

"Glen Able" <(E-Mail Removed)> wrote in message
news:ccjusj$b4s$1$(E-Mail Removed)...
> 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.
>


OK, I've had another go at implementing this...


Here's problem #1:

Clearly I need to have this constructor:

Float(float value) { m_value = value; }

But the existence of this means that any unions containing a 'Float' will be
invalid, whereas they were OK with 'float'.


Problem #2 seems more fundamental:

To allow sqrt etc. to work, I seem to require

operator float() { return m_value; }

Which causes massive problems of ambiguity regarding conversions.



By the way, would anyone else care to give this substitution a quick try on
their own code (so, define the Float class, then redefine float as Float)?
I think it'd be an interesting exercise to see what other issues this throws
up, especially since so many people seem to think this should be
unproblematic!

cheers,
G.A.


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-09-2004
E. Robert Tisdale posted:


> #define float Float



Disgusting.


We have:

inline functions
global const variables
typedefs
tempates

for a reason.


typedef float SpecialFloat;
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-09-2004
Glen Able posted:

>
> "Glen Able" <(E-Mail Removed)> wrote in message
> news:ccjusj$b4s$1$(E-Mail Removed)...
>> 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.
>>

>
> OK, I've had another go at implementing this...
>
>
> Here's problem #1:
>
> Clearly I need to have this constructor:
>
> Float(float value) { m_value = value; }
>
> But the existence of this means that any unions containing a 'Float'
> will be invalid, whereas they were OK with 'float'.


No problem there. There'd be a problem with virtual functions though... but
ofcourse that isn't a virtual function.


union
{
SpecialFloat j;

int p;
} cow;


cow.p = 42;

cow.j = 45.6;


>
> Problem #2 seems more fundamental:
>
> To allow sqrt etc. to work, I seem to require
>
> operator float() { return m_value; }
>
> Which causes massive problems of ambiguity regarding conversions.



I don't see what you're on about. If a function wants a float:

SpecialFloat t;

Func(t);

What's wrong with that?

Or:

SpecialFloat k;
float t;

float r = k + t;

Again here, it's converted to a float



> By the way, would anyone else care to give this substitution a quick
> try on their own code (so, define the Float class, then redefine float
> as Float)? I think it'd be an interesting exercise to see what other
> issues this throws up, especially since so many people seem to think
> this should be unproblematic!
>
> cheers,
> G.A.


Did my own example not show that? This is kindergarden stuff.


-JKop
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
any MCDST exams can substitute 070-270 exam in MCSE? Daud Christian MCSE 30 05-30-2005 06:37 PM
Techie Re: any MCDST exams can substitute 070-270 exam in MCSE? Seelan MCSE 0 05-27-2005 03:25 PM
substitute for nested select query DC Gringo ASP .Net 6 05-03-2005 08:59 PM
Substitute UC with space UC flamencoman@earthlink.net Perl 1 03-07-2004 08:02 PM
3rd party reporting tools to substitute Crystal Matalote ASP .Net 2 01-26-2004 04:46 PM



Advertisments