SRL Archive - OSR Help

FloatToInt


jimthesoundman

2010-02-13 02:09:22

Since there is apparently no FloatToInt or vice versa, then is the only way to accomplish this to convert the float to a string, and then convert the string to an integer? If someone knows a simpler way to do this I would be very grateful.

JIM

Quickmarch

2010-02-13 02:44:56

Round()?

void_hatred

2010-02-13 03:00:21

what are you trying to accomplish?

for example if I were to say, FloatToInt(3.14) would you want an output of 3?
if so, last night I was mucking around with SCAR, and looked at the docs, and there is some neat math functions, e.g. Round, Trunc etc.

hope that kind of helps (i'm really new, don't hate me if I'm wrong :))

EDIT:
I never refreshed my browser, it appears I got "ninjad" (by quarter of an hour, sorry)

void_hatred

Gu3st...

2010-02-13 03:35:16

function Round(e: Extended): LongInt;
Rounds and converts floating point number to integer.

function Trunc(e: Extended): LongInt;
Rounds down and converts to integer.

function Int(e: Extended): LongInt;

function Floor(X: Extended): Integer;
Rounds down X to the lowest integer.
Example:
Floor(5.4) = 5

function Ceil(X: Extended): Integer;
Rounds down X to the highest integer.
Example:
Ceil(5.4) = 6
:rolleyes:

Quickmarch

2010-02-13 03:51:06

From: Zyt3x
function Round(e: Extended): LongInt;
Rounds and converts floating point number to integer.

function Trunc(e: Extended): LongInt;
Rounds down and converts to integer.

function Int(e: Extended): LongInt;
:rolleyes:


Don't forget Ceil(). ;)

Markus

2010-02-13 16:18:15

One small thing: Trunc and Floor aren't completely the same. Trunc just deletes the decimals, and floor rounds it down.
So what's the difference, you may ask?
Lets take a negative number, like -1.2, trunk makes it -1, but floor will round to the lowest (= -2).
just a bit of 'i-can-script-too' xd

senrath

2010-02-13 16:22:06

Negative numbers are the only things that'll make Floor and Trunc act differently, right?

Markus

2010-02-13 18:12:19

Yup, that's correct.

jimthesoundman

2010-02-15 04:55:41

From: void_hatred
what are you trying to accomplish?

for example if I were to say, FloatToInt(3.14) would you want an output of 3?
if so, last night I was mucking around with SCAR, and looked at the docs, and there is some neat math functions, e.g. Round, Trunc etc.

hope that kind of helps (i'm really new, don't hate me if I'm wrong :))


My main reason for asking is for progress reports. If I want to determine exp per run, or exp per hour, or something like that, if I divide an int, by an int, I don't get an accurate answer. The answer will be truncated. 20/30=0 and stuff like that, which obviously doesn't work. So if I have an int, lets say experience, and I multiply that by a float, lets say exp per bar, as in smithing which might be something like 37.5, so you will end up with a type mismatch error, as you can't do anything with floats and ints together, they just don't comingle well. So the solution I've found is to convert the integer to a string, then convert that string to a float. So instead of my runs counter being an integer as it was originally, it is now a float and I can multiply it by my exp per bar, which is also a float, so that will tell me the correct amount of total exp I have earned while this script was running.

Hopefully this all makes sense…

JIM

void_hatred

2010-02-15 05:34:10

It sounds like you want IntToFloat, not FloatToInt (though I can't find IntToFloat being a function)

however, you could just define both variables to be extended, for example:


program Jim;

var
exp, logs: Extended;

begin
exp := 37.5; // per tree, etc.
logs := 5;
WriteLn(a * b); // 187.5
end.

void_hatred