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

Volker Wysk <[email protected]>
Newsgroups gmane.comp.lang.mercury.general
Message-ID <[email protected]>
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
> [email protected]
> https://lists.mercurylang.org/listinfo/users

_______________________________________________
users mailing list
[email protected]
https://lists.mercurylang.org/listinfo/users
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmTfL78ACgkQ+K4ydFOs
HoUXKBAAoMmuDxioiZhaBHs/LVJkxItllsO6bfbMQ+BDSBkvurwV/MSpfDzJB996
+djoMf3tlWZ+ni36Gk8QI2qqlwMvWhWdGaco3dEILqTQhyW5GU1FWAc+ZaPuqPP4
V5U4DOagGY9INbTR6nHqPLMLP4lRocbjyT7C84l2BUwy0lyh6qYP3hDuhxXasAvh
CWu4df9i0hYBxnSLOruZTvdslCP3JXaaEpfrOU1cBkUdTcPTFiuqrFZG8P4JOEWu
YxNXnZcOwX9/NzTPW/KRJ14enkPj1sW+FisXEmVOsNRQeu5F1vQ4kGHQV3R/ZjmN
3J0T8M3pIbfl2yDedPRuiKjrZbJLWKQSIfaHT+I8iYcZ1DejD05AtLt6pHNJn4BK
Y3r6dyiWyADnJ94BfV3L9s8zbJqQzRNUzmNmkwqnrZDEhpM92y0IJyiqlwX3F66B
jWqm17+KZOSu/PweNHwdL4Zmzg4yzTLATp2KZ3OlhsTWslZuj9h+qMh12ZpILtpC
nZT55+RDVQwrlxp7lkr35eu8f0Urekg/tXBEUHbFQcXczsTV2a1jyxwUMxaP8V38
B5h5V092Qj0s0Qu05AcV1s8Fyj+ZghRNV5ItqMOghn1k3tYF9CRVoKwaXsn5k2CD
sp4Hixx2fi1HWHUnwfWPSrP/c1VJ97WEUfb6kvK3cCPOWnywT50=
=JIvi
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.