Small floating point numbers

SGDK only sub forum

Moderator: Stef

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Small floating point numbers

Post by matteus » Thu Sep 15, 2016 8:13 pm

I've a few numbers that are .8, 1.2, etc and was wondering how I'd use those in the SGDK? :)

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Small floating point numbers

Post by Stef » Fri Sep 16, 2016 11:37 am

You should have a look on that :
https://github.com/Stephane-D/SGDK/wiki/Tuto-Maths

:)

Miquel
Very interested
Posts: 514
Joined: Sat Jul 30, 2016 12:33 am

Re: Small floating point numbers

Post by Miquel » Fri Sep 16, 2016 11:55 pm

"Serious" games usually use fixed points instead of floating points in game logic because they are way faster. You should use them for things like "position" and "speed", there is a notable difference in response. The big handicap is you no longer have so many decimals, but is strange to use them in great amount in games. So no big deal about not having floating points after all.
HELP. Spanish TVs are brain washing people to be hostile to me.

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Mon Sep 19, 2016 4:58 pm

It's based on old ZX spectrum basic which seems to allow for float point numbers in some weird way!

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Mon Sep 19, 2016 6:11 pm

if anyone knows how you'd write this without floating point as an SGDK equivalent I'd be very impressed (grateful)!

Code: Select all

round((sqrt(u16Popularity + u16EnergyCommitedPerTourDate) * 1000) / 120)

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Mon Sep 19, 2016 6:56 pm

Having had a good look at the maths support I don't think it's possible for me to do with fix16 via the math functions as u16Popularity goes up to 100 and u16EnergyCommitedPerTourDate goes up to 1000.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Small floating point numbers

Post by Stef » Mon Sep 19, 2016 7:03 pm

matteus wrote:if anyone knows how you'd write this without floating point as an SGDK equivalent I'd be very impressed (grateful)!

Code: Select all

round((sqrt(u16Popularity + u16EnergyCommitedPerTourDate) * 1000) / 120)
Should be equivalent :

Code: Select all

u16 v = fix16ToRoundedInt((fix16Sqrt(intTofix16(u16Popularity + u16EnergyCommitedPerTourDate)) * 100) / 12);
But it would be better to use fix16 type variable directly when you need it.

You also need to enable the maths table in SGDK library, change following line in config.h from SGDK:

Code: Select all

MATH_BIG_TABLES 1

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Mon Sep 19, 2016 7:13 pm

Thanks! and this one

u16Happiness = u16Happiness + Round(((u16Popularity * 10) / 1000) / 4)

I'm guessing I don't need to change as a standard int will just truncate? lol

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Mon Sep 19, 2016 7:14 pm

Code: Select all

main.c|| undefined reference to `sqrttab16'|
main.c|| undefined reference to `sqrttab16'|

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Small floating point numbers

Post by Stef » Tue Sep 20, 2016 1:28 pm

Did you recompiled the library with the

Code: Select all

MATH_BIG_TABLES 1
change i mentioned above ?

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Tue Sep 20, 2016 1:55 pm

yes I've changed the flag, I tried recompiling the libraries as well

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Small floating point numbers

Post by Stef » Tue Sep 20, 2016 5:57 pm

If you correctly recompiled the library with MATH_BIG_TABLE enabled (set to 1) then you should not meet this error.
How are you compiling your project, command line ? code::block ?

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Tue Sep 20, 2016 6:09 pm

I use code::block

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Small floating point numbers

Post by Stef » Tue Sep 20, 2016 8:20 pm

Did your libmd.a file was correctly regenerated ? try to remove it before compiling the library so you will be sure it will be correctly rebuild :)

matteus
Very interested
Posts: 336
Joined: Mon Feb 04, 2008 1:41 pm

Re: Small floating point numbers

Post by matteus » Wed Sep 21, 2016 9:25 pm

I've deleted it and get the error

||=== Build: release in MegaRockBandManager (compiler: Sega Genesis Compiler) ===|
sgdk\lib\libmd.a||No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|

Post Reply