[m-users.] Best way to implement constants ?

Volker Wysk post at volker-wysk.de
Fri Aug 18 18:45:51 AEST 2023


Hi, Sean

Why don't you just use the "/" operator from the float library?

Volker


Am Freitag, dem 18.08.2023 um 09:26 +0100 schrieb Sean Charles
(emacstheviking):
> I am currently working on a simple 2D game engine using raylib, all good.
> I'm implementing a tweening library based on -the- easing equations by
> Robert Penner.
> 
> The ones I need I have implemented, but I don't think I will need the
> complete set. On implementing the easeInBounce, easeOutBounce and
> easeInOutBounce, using this as my guide, the raylib Rust easing
> source: https://docs.rs/raylib/latest/src/raylib/ease.rs.html#242-256 , I
> noticed that there are some constants which I decided I didn't want to
> have to recalculate at run time.
> 
> In a game loop, anything you can do to save pointless repetition means
> more rendering time between frames.
> 
> This MIGHT be over eager optimisation I guess as all I am doing at the
> moment is a simple proof-of-concept for a 2D engine which I will then port
> over as the core of my transpiler IDE I have started.
> 
> Here is what I ended up for as the implementation of easeOutBounce, I
> decided to use the apostrophe naming rule so that the code reads like the
> source code, and I also did this because I wasn't really sure what those
> constants are for, I have a rough idea but I couldn't decide on an
> effectively communicable name for the source so I opted to call them what
> they do!
> 
> 
> tween_(bounce_out, Time, Range, Duration) = V :-
>     Percent = Time / Duration,
>     ( if Percent < '1 / 2.75' then
>         V = Range * n1 * Percent * Percent
> 
>     else if Percent < '2 / 2.75' then
>         Td1 = Percent - '1.5 / 2.75',
>         V = Range * ( n1 * Td1 * Td1 +0.75 )
> 
>     else if Percent < '2.5 / 2.75' then
>         Td1 = Percent - '2.25 / 2.75',
>         V = Range * ( n1 * Td1 * Td1 +0.9375 )
>     else
>         Td1 = Percent - '2.625 / 2.75',
>         V = Range * ( n1 * Td1 * Td1 + 0.984375 )
>     ),
>     trace [io(!IO)] (
>         io.format("bounce_out: %f\n", [f(V)], !IO)
>     ).
> 
> The above code then make suse of the following constants, I read somewhere
> that small functions are automatically inlined (but not 100% sure I read
> it) so I explicitly made then inlined, the idea being that at runtime,
> referential transparency would be applied, like in Haskell...
> 
> 
>     % Bounce constants.
> 
> :- pragma inline(func(n1/0)).
> :- func n1 = (float::out) is det.
> n1 = 7.5625.
> 
> :- pragma inline(func(d1/0)).
> :- func d1 = (float::out) is det.
> d1 = 2.75.
> 
> :- pragma inline(func('1.5 / 2.75'/0)).
> :- func '1.5 / 2.75' = (float::out) is det.
> '1.5 / 2.75' = 0.5454.
> 
> :- pragma inline(func('1 / 2.75'/0)).
> :- func '1 / 2.75' = (float::out) is det.
> '1 / 2.75' = 0.3636.
> 
> :- pragma inline(func('2 / 2.75'/0)).
> :- func '2 / 2.75' = (float::out) is det.
> '2 / 2.75' = 0.7272.
> 
> :- pragma inline(func('2.25 / 2.75'/0)).
> :- func '2.25 / 2.75' = (float::out) is det.
> '2.25 / 2.75' = 0.8181.
> 
> :- pragma inline(func('2.5 / 2.75'/0)).
> :- func '2.5 / 2.75' = (float::out) is det.
> '2.5 / 2.75' = 0.909.
> 
> :- pragma inline(func('2.625 / 2.75'/0)).
> :- func '2.625 / 2.75' = (float::out) is det.
> '2.625 / 2.75' = 0.9545.
> 
> My question is simple this: is what I have done 'ok' or is it a diabolical
> insult to all that as beautiful and holy in the Mercury world?
> Could I have used some other aspect / feature of the language?
> 
> Thanks
> Sean
> 
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20230818/62bec7a2/attachment.sig>


More information about the users mailing list