Re: [m-users.] Best way to implement constants ?
Volker Wysk <[email protected]>
| Newsgroups | gmane.comp.lang.mercury.general |
|---|---|
| Message-ID | <[email protected]> |
Am Freitag, dem 18.08.2023 um 09:55 +0100 schrieb Sean Charles
(emacstheviking):
> >
> > I mean, constants like "1 / 2.75" will sure be evaluated at compile
> > time.
> > And even if not, it's just a division, nothing expensive.
>
>
> Do you know where in the docs it says that will happen?
No, I don't. Sorry.
> I couldn't find anything as reassurance, and I assumed then that a
> floating point division would take place, at that point, every time, which
> feels inefficient, given that it is in fact a constant and should require
> zero calculation at runtime.
>
> Expensive is relative I guess. I come from the days when a 1MHz CPU was
> considered exotic.
Cheers,
Volker
>
>
> >
> > Volker
> >
> > >
> > > 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
> >
> > _______________________________________________
> > 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+K4ydFOsHoUFAmTfMskACgkQ+K4ydFOs HoUZ0xAA3WEhRi8T4Wz4OJH/yAlSV1CQQnORMVj7pcQvCCNjZcCWhi4bi6Sdi2dq sefs6z8d51+IaCerk3haEpoeN1FqIhTuEDfOfi2pqr3b4jTr1g+/Ucn8qyjSnavO p3KhND9boa9gz9LuBWmt+/jckKeSeQ1VlMUBKD2TLkrO/yF2L+/fzwxPwvJ55Ke8 DO5lmouS/y0l2iZhM8cKGqi3JDgQJGUyDC+anyvn9jKtsRCmNvxdHTXr/qOxlomX yqvXEiuxia/YRaQNJwIzepw6PbfRELvqnxe6gymrDFNFMd9mr99aFICx1nI8l3vp kzKkrCu18dxv5739+J7FhL3aIWkEcGRUKrDIlEmf78Pk4KpPSptl2eBxBk+X4B2Y S83V6EP4a8hov6fnDnQnuZrksecHb94rYq01j+82RTTcqzG0EU8et7zxrmCWEl2B HLMgdM90KB311sgc6EBKM6WYijUnCnvbD3T7ZjfbFn+NRpVUsZUCHrjJcIz/bayr xcUR99VgtlRRAJx0en8zB6QxZr8rpPTeiQh5Z8w7Dz2F4mcd8PC62FQQZ/OUEJkp ipglmvdL7kbUoqV+3TSC3G8YAPNG73U2ZhRJDdEDtw37MVpQd6MTq4AJOpPEUrfI pb//uWcqLaOR6Anr4d2WwZ6E4tsETlE8rfvzIMUmN4sUg7/4vS4= =3cKG -----END PGP SIGNATURE-----