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 10:45 +0200 schrieb Volker Wysk:
> Hi, Sean
>
> Why don't you just use the "/" operator from the float library?
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.
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
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmTfMPMACgkQ+K4ydFOs HoXjuhAAm3QKcgZXxVb1Of3CAB1vCW3JWbaTSEI7UpjgUaQmmwBO9FfSXH0TKQ7A wNd2i/p7gkdPiubyehxSwrhmgeCkg2+5MlDeBGLWvJHo5BMnafpE7wf+NBp8rdAs qNWdDlSLkNwKUAbZbvCmC1+me20CFq10nwBRheaHl+0+3kUe4ESkOKgfucSTCpNZ gph+XOvZ/RN96PwJsilqKJEv7wRhvtKg6zG64WC9tEwA1/5sF/WONUz7bmZB2XaN 4bC52uRHWoEs2SvGgb0cEwWjUPC0pTHL13B2ioikdPH4buoDBsyqCPO+VFwYdpZ2 W1M92zozKfrhnJ5DmGbZnhgLJUXcESGk6l3MHq40oRaCiLEhQYhoegty7tYzclMK xT8SZjl/IvW2/nJ+Dfim/jOq7beO98zZ1oyNB8zfEeOGL2qKEbHcD1XAtnjawfyq Ew/qLJvqYhBrbabI3vV05+SftlQHF6mTnIt/HDr3NIaNM0Eh0T6bxw3A9fpeZ0MM Nvp7xHnHU3m5aeZWE4Ajx7jkyOAlqi35M8cIYm7p+1Jg+393EH77yNoWe/bt4j81 +yWnrbEj3QjvdJnbY3OTFen5wAO1IgZPtX6xrWTsfB/MY34XiSpTVgvJEgIxfsoe WoYAojtY73HGALO8b52c3ds3/G5ojZgIBGC0RbyURDLn7bTvDSE= =1utt -----END PGP SIGNATURE-----