gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Class Float instead of typedef f32 ??

#72251 - ishraam - Fri Feb 17, 2006 5:58 am

Yo,

I'd like your opinion. And forgive me if that's somewhere stupid (it's 5.55 am I haven't had much sleep recently :p).

I was telling to myself, after reading many different threads, that working with f32 could quicly become a pain in the ass. Even writing the Vector3 class for f32 is boring (especially when it's not your first nor second nor third....time). Obviously, struggling with f32 helps a lot to get a better understanding of how things work, but once you got it...well you got it :).

When you try to keep a clean and readable code, and moreover when you want (easily) portable code , or to port some to the DS (cf. all the "doomDS", "QuakeDS",...), having to deal with "f32" and "mulf32" instead of "float" and " * " is "not friendly" (read: a little pain in the ass).

So an idea poped out of my mind : why not create a "Float" class around the f32, which redefines every operators (even the bit shifting), and has everything inlined?? Porting your C++ well written classes would then (almost) be nothing but a matter of find & replace "float" by "Float" (anyway, that's the idea behind it)

Basically:
Code:

struct Float
{
   f32 val;
   
   //::::CONSTRUCTORS
   
   Float():val(inttof32(0)){}
   Float(float parVal):val((parVal)){}
   Float(f32 parVal):val(parVal){}
   Float(const Float & parVal):val(parVal.val){}


   //:::OPERATORS REDEFINED
   
   Float operator + (const Float & parF)
   {
      return Float(val+parF.val);
   }

   Float operator * (const Float & parF)
   {
      return Float(mulf32(val,parF.val));
   }

};


Having everthing inlined, it should not be slower that f32, and might be worth the writing, IMHO.
I have been in DS dev only for 1 week, so I might be totally wrong.

Anyway, what's your opinion ?

#72262 - DekuTree64 - Fri Feb 17, 2006 6:50 am

Yeah, that's what we do where I work, and it does make the code much more readable.

Just don't go encapsulation-crazy on it and try to hide the raw value entirely. Sometimes it can save a lot of headache having it public, so you can shift around manually during a sequence of operations to maintain as much accuracy as possible. Looks like you've already figured that out, seeing as it's a struct, but I figure I'll throw it out there for the benefit of others reading :)
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#72266 - ishraam - Fri Feb 17, 2006 7:25 am

Quote:
Yeah, that's what we do where I work[...]

Nice to hear it ^_^.

Quote:
I figure I'll throw it out there for the benefit of others reading :)

That would be nice too.

If you (or anyone else) have other little "tricks" like this one up in your sleeve, I'd be glad to hear them!

(I am sure everyone has his little tricks, that helps in a way or another; maybe enough to create a useful "sticky" like post, who knows ^_^.)

[EDIT]

For example, I am not sure wheter wifi libs are available yet (I think they are), but through wifi, you could do pretty useful things.
Ever thought of a Log class that would log everything on a sever (your pc), through wifi ? Being able to search through compete logs could really save some precious time (and energy), especially on massive projects.

(BTW, there is a really great article on Gamedev about XML & logs - and pretty useful javascript-xsl tools)

#72308 - genfish - Fri Feb 17, 2006 3:40 pm

would ya give an example of it being used?

also should it be class, not struct?

im not the sharpest tool in the box so i might be wrong :)
_________________
there is no rl only afk

#72316 - acox - Fri Feb 17, 2006 4:57 pm

genfish wrote:
would ya give an example of it being used?:)

Code:

Float a = Float(1.1f); // Explicit
Float b = 1.2f;
Float c = 0;

c += a * b;

a = (a * 21 + b * 45 + c * 1.1f) / a;


I think the float mixed in there will be ok: '*' expects 2 Floats but Float has a constructor that takes a float which will be invoked behind the scenes. As the float is a compile-time constant, all floats will compile out.

genfish wrote:
also should it be class, not struct?


"struct" in C++ is just the same as "class { public:".


Cool. I never did Fixed-point except in straight C/Assembly, but it looks like it will all inline and compile down to the same thing in C++ anyway :).
_________________
3D on GBA

#72373 - tepples - Sat Feb 18, 2006 12:58 am

The name Float for a fixed-point class is confusing. Use Fix or Fixed instead.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#72388 - ishraam - Sat Feb 18, 2006 4:48 am

Yep, you're right Float might be confusing.
...I'll stick to it anyway :p

#72393 - LOst? - Sat Feb 18, 2006 6:55 am

I know what I would like to have: A Fixed float class, and a sting manipulation class.

I would love to make them myself, but I have never made classes that handles overloaded operators. I am about to learn it.
_________________
Exceptions are fun

#72484 - sajiimori - Sat Feb 18, 2006 10:15 pm

What the hell is a Fixed float class?? heheh