Rounding up numbers

Strainer
15 Dec 2013, 15:21
Hello,

as I'm currently working on a converter between american and european height measurements, I would need a round function aswell. I thought of simply using RTrim to trim the numbers as strings, but I thought there could have been some better alternative there.

So far, I have the following function in Quest:

<function name="HeightConversion" parameters="cmvalue" type="int">
inchvalue = cmvalue * 0.3937
feetvalue = inchvalue * 0.08333333
msg (inchvalue)
msg (feetvalue)
</function>


So my next steps would be:

Cutting the feetvalue to a single digit number (integer) and multiply the result by 12 to get the inch-value.
Substract that inch-value from the variable "inchvalue".
That should give me the height as feetvalue' inchvalue".

I myself am european and use the metric system, but I would like to make something similar to an engine for RPG-elements, and being able to show values such as height and weight in both forms seems like a good idea.

george
15 Dec 2013, 16:07
This is the function that Alex originally posted:


define function <round>
set numeric <round.number; $parameter(1)$>
set numeric <round.dp; $parameter(2)$>
set numeric <round.decimalpos; $instr(%round.number%; .)$>
if (%round.decimalpos% = 0) then {
return <%round.number%>
}
else {
set string <round.beforedp; $left(%round.number%; {%round.decimalpos%-1})$>
set string <round.afterdp; $mid(%round.number%; {%round.decimalpos%+1})$>
set string <round.afterdp; $left(#round.afterdp#; %round.dp%)$>
return <#round.beforedp#.#round.afterdp#>
}
end define


Quest doesn't use that specific syntax to declare functions anymore, but you should be able to follow the logic and write that function with the current version of Quest. If you have any specific questions (or just don't know where to start) ask away.

Note that you'll need these functions:

http://quest5.net/wiki/If

http://quest5.net/wiki/Instr

http://quest5.net/wiki/Left

http://quest5.net/wiki/Mid

http://quest5.net/wiki/Return

http://quest5.net/wiki/Set (read the note about when you need set versus declaring attributes explicitly)

jaynabonne
15 Dec 2013, 16:26
You have three rounding functions:

floor - rounds down to the next lowest integer
ceiling - rounds up to the next highest integer
round - rounds up or down depending on whether the fractional part is greater or equal to 0.5.

      x = 3.14
msg(floor(x))
msg(ceiling(x))
msg(round(x))

3
4
3

     x = 3.6
msg(floor(x))
msg(ceiling(x))
msg(round(x))

3
4
4
      x = -3.14
msg(floor(x))
msg(ceiling(x))
msg(round(x))

-4
-3
-3
      x = -3.6
msg(floor(x))
msg(ceiling(x))
msg(round(x))

-4
-3
-4

Hope that helps.

jaynabonne
15 Dec 2013, 16:29
If you want to simply discard the fractional part, you can also do this:

x = 3.14
msg(cast(x, int))

3

x = -3.14
msg(cast(x, int))

-3

george
16 Dec 2013, 00:04
@jay, that's funny, I looked on the wiki for Floor, ceiling, etc. but didn't see it. Are they there? Is the documentation somewhere where someone could post it to the wiki?

That makes me wonder if there are other functions not on the wiki?

jaynabonne
16 Dec 2013, 07:37
The expression parser used in Quest is FLEE. The docs are here:

https://flee.codeplex.com/

But even those docs don't list floor and ceiling. I believe it exposes standard C# functions.

Here's a forum post I had done after doing some experimentation:

viewtopic.php?f=18&t=3595

george
16 Dec 2013, 15:37
indexes!! Holy toledo! :D

edit: hmm, so I'm not sure -- if we use FLEE functions, are they not converted to JS if we turn our games into mobile apps?

jaynabonne
16 Dec 2013, 22:23
That I don't know, to be honest. Alex would know better about specific functions.

Alex
17 Dec 2013, 11:59
Quest imports the .net Math functions: http://msdn.microsoft.com/en-us/library ... .math.aspx

Most of these are available in JavaScript so no problems there.

I would avoid using "cast" though, as that's a FLEE function and won't be available in JavaScript.

The Pixie
17 Dec 2013, 16:40
I had a play around with these, and write a Wiki page about it:

http://quest5.net/wiki/Use_maths_functi ... Operations

george
18 Dec 2013, 05:00
That's awesome Pixie.

I've already started my geometrical RPG btw...