Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBC/DBP] Round Up Variables

Author
Message
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Sep 2007 19:51 Edited at: 11th Sep 2007 19:03
DB always truncates floats when turning them into integers, so I made an amazingly simple function to round to the nearest integer. Well it seemed simple at the start, but eventually I got the correct formula. Thanks for your help guys



Rounding to the nearest integer is more mathematically correct in all situations so I hope everyone finds this useful

In programming, nothing exists
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 1st Sep 2007 21:19
Lol... I needed such a function today, and wrote my own, but that's much more complicated.



So.. thanks, your function will probably run a lot faster.

Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 2nd Sep 2007 00:00
I wrote almost that exact function once, but IanM pointed out that it doesn't work with negatives.



IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 2nd Sep 2007 00:02
@Obese,
Your function is broken - when I put in minus 4.2, I got a result of minus 3.

@Mr Kohlenstoff,
Yours works, but it has a lot of overhead that's simply not needed.


- Hopefully, that's also DBC compatible.

Also, it's being a little pedantic, but this is really a 'round to nearest integer', not round up.

DBPro already has them, but here's a floor and ceiling function that should also be DBC compatible:


Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 2nd Sep 2007 02:55 Edited at: 2nd Sep 2007 03:16
I called it round up because DB always rounds down.

Quote: "Your function is broken - when I put in minus 4.2, I got a result of minus 3."

wtf?
[edit] I tried it and it worked fine.

Sorry guys I hadn't tested this, I wrote a really long function a while back and just looking at it I came up with this and thought it was so simple it must be right
I'll try and find the most efficient way of doing it, including negatives.

[edit]
Dang! I can't do it without using an IF statement
There must be a mathematical trick to get -.5 from a negative number and .5 from a positive.

Your signature has been erased by a mod because it was rubbish.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 2nd Sep 2007 03:23
Here's the best I can do.


Your signature has been erased by a mod because it was rubbish.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 2nd Sep 2007 08:23 Edited at: 2nd Sep 2007 08:27
IanM

Quote: "@Obese,
Your function is broken - when I put in minus 4.2, I got a result of minus 3."


What result did you expect? I got the same result. In working with negative numbers, the lower the number, the higher the position in the ladder - so minus 3 as calculated by OBese87’s function is UP from minus 4 as calculated by DBClassic.

Run through the following and check out by the time you get to minus 4.6, your method has calculated minus 5 as against OBese87’s minus 4.
Whilst OBese87’s minus 4 is not UP from minus 4 as calculated by DBClassic, it is up from the original number and your minus 5 is DOWN from both.




gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... Some Eskimos have been known to use refrigerators to keep their food from freezing.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 2nd Sep 2007 08:35 Edited at: 2nd Sep 2007 08:39
Sorry! Duplicate posting


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... Some Eskimos have been known to use refrigerators to keep their food from freezing.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 3rd Sep 2007 21:06
First, let's just check the original function - it adds 0.5, which means that it rounds to the nearest integer when given a positive number. If it was going to always round up, then it would need to add 'almost 1.0', or 0.99999~

What the intent of the code was and what the title specified are two different things, and as as OBese87's final code post provides 'rounding to nearest', I appear to be on the same wavelength, as I pointed out in my original post.

Next, how can rounding -4.2 in any way, give you an answer of -3? Rounding implies up to the next integer, or down to the next integer, or nearest integer, not skip one and then get the next. -4.2 can however be rounded 'up' or 'nearest' or 'towards zero' to -4, or 'down' to -5, three of which I put in my post (RUP = nearest, MyFloor = down, MyCeil = up, int = towards zero).

The DBPro int function does not round down, which was OBese87's original assumption - it simply truncates. I would assume that DBC does the same, although I haven't tested it as I haven't had DBC installed for a couple of years.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 3rd Sep 2007 22:05
That's what I meant "truncates" (that means it just ignores numbers after the decimal point right?)

I called it rounding-up as that's what I was taught in school, I thought it was rounding up because 0.5 is in the middle but rounded up to 1.

In programming, nothing exists
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 4th Sep 2007 12:29 Edited at: 5th Sep 2007 03:53
IanM

Sorry! I don’t have DBPro so am unfamiliar with its workings. However, I was under the impression OBese87 wanted to round to the nearest high integer, with both positive and negative numbers going in the same direction. Adding 0.5 will always round to the nearest high integer, regardless of the sign of the starting value. If you add 0.5 to 4.2 you get 4.7 (nearest integer 4) and if you add 0.5 to -4.2 you get -3.7 (nearest integer -3). Likewise, subtracting 0.5 will always round to the nearest low integer, regardless of the sign of the starting value. If you subtract 0.5 from 4.2 you get 3.7 (nearest integer 3) and if you subtract 0.5 from -4.2 you get -4.7 (nearest integer -4).

Code Dragon

What results did you get from your function when you entered a negative number?


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... Some Eskimos have been known to use refrigerators to keep their food from freezing.
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 8th Sep 2007 03:05 Edited at: 8th Sep 2007 03:07
What? No reply?


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... Some Eskimos have been known to use refrigerators to keep their food from freezing.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 9th Sep 2007 02:19 Edited at: 9th Sep 2007 02:20
Quote: "Adding 0.5 will always round to the nearest high integer, regardless of the sign of the starting value. If you add 0.5 to 4.2 you get 4.7 (nearest integer 4) and if you add 0.5 to -4.2 you get -3.7 (nearest integer -3). Likewise, subtracting 0.5 will always round to the nearest low integer, regardless of the sign of the starting value. If you subtract 0.5 from 4.2 you get 3.7 (nearest integer 3) and if you subtract 0.5 from -4.2 you get -4.7 (nearest integer -4)."

?
The point is that nearest integer after adding or subtracting 0.5 is irrelevant. The nearest integer of the original value is what we're looking.


Quote: "Dang! I can't do it without using an IF statement
There must be a mathematical trick to get -.5 from a negative number and .5 from a positive."


Here's the trick:


Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 9th Sep 2007 02:44
Quote: "Here's the trick:

..."


Isn't it basically the same? The "(n#>0)" is handled kinda like an if-statement, isn't it? I mean, it shouldn't increase the performance or so, or is maybe even slowlier, because calculations take more time then if-selections, as far as I know.

empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 9th Sep 2007 03:15 Edited at: 9th Sep 2007 03:20
Quote: "Isn't it basically the same? The "(n#>0)" is handled kinda like an if-statement, isn't it? I mean, it shouldn't increase the performance or so, or is maybe even slowlier"

How can be slower if it's basically the same?
It's slightly faster in DBC.

Quote: "because calculations take more time then if-selections, as far as I know."

The problem with DBPro is that type conversions are rather slow, so I wouldn't be surprised if it's slower there but I haven't tested it.

empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 9th Sep 2007 03:59 Edited at: 9th Sep 2007 04:00
Well I did a few tests and in scenarios like this:


both functions perform roughly the same.

If we change Round2 to return an integer value and omit the Int() function calls it will get significantly faster


We'd get another speed increase if we use Round1() inline which is entirely possible since it's only one line of code. Although it's prolly not nice to read.

gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 10th Sep 2007 08:03
If, according to empty

Quote: "The point is that nearest integer after adding or subtracting 0.5 is irrelevant."


why then are we doing a calculation at all?

and

Quote: "The nearest integer of the original value is what we're looking. "


On that basis, what is the nearest integer of these original positive values
4.9 : 4.8 : 4.7 : 4.6 : 4.5 : 4.4 : 4.3 : 4.2 : 4.1
and of these original negative values
-4.9 : -4.8 : -4.7 : -4.6 : -4.5 : -4.4 : -4.3 : -4.2 : -4.1

Using int(), the answer is 4 and -4

Regardless of what IanM said in his post,

Quote: "What the intent of the code was and what the title specified are two different things"


I am still under the impression that OBese87 simply wanted to round to the nearest high integer, with both positive and negative numbers going in the same direction, whether it be UP or DOWN, but not both ways.

Also we seem to be getting off track as speed has nothing to do with OBese87’s original post.


gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Sep 2007 13:19 Edited at: 19th Sep 2007 01:15
What I want to happen is:
4.2 = 4
4.5 = 5
4.8 = 5
-4.2 = 4
-4.5 = -5
-4.8 = -5

The negatives are correct aren't they? or would -4.5 = -4?

In programming, nothing exists
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 10th Sep 2007 13:50
Quote: "On that basis, what is the nearest integer of these original positive values [...] and of these original negative values [...]. Using int(), the answer is 4 and -4"

Like Ian said, the int() function truncates the floating point value, leaving you with the integer part. And like Obese87 wrote, the *nearest* integer of a value X where (-)4.0 > X < (-)4.5 is (-)4 and where (-)4.5 >= X < (-)5.0 is (-)5.


Quote: "
I am still under the impression that OBese87 simply wanted to round to the nearest high integer, with both positive and negative numbers going in the same direction, whether it be UP or DOWN, but not both ways."

Apparently not.

Quote: "Also we seem to be getting off track as speed has nothing to do with OBese87’s original post."

If you're going to implement such a routine in your program, efficiency and speed should be a concern.

On a side note (and this is off topic ):
In assembly you'd use FIST or FISTP to convert a floating point value to an integer. Depending on the mode of the FPU it either truncates or rounds the value. However, if in "round mode" it rounds to the nearest even integer (following the IEEE standard). That means 3.5 becomes 4, and 4.5 becomes 4 as well.

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th Sep 2007 22:59 Edited at: 10th Sep 2007 23:39
@WK
That's how my second one works, I just tried to do it mathematically which probably takes up more memory and is also slower
Why can't I find this trick?

[edit]
I've done it!
Can anyone explain the strange things happening to the "test#" variable when it gets around zero.
My function still works through it though
I wanted to use a FOR loop, but I can't get the darn thing to work with a 0.1 increment; is that possible?
Thanks guys, you were all a lot of help, especially when arguing with each other



In programming, nothing exists
gearce
17
Years of Service
User Offline
Joined: 18th Dec 2006
Location: ex SCOTLAND, now MELBOURNE, Australia
Posted: 11th Sep 2007 07:21 Edited at: 11th Sep 2007 07:25
My final say.

I stand by everything I have said.

OBese87’s original function



added 0.5 to the input number, positive or negative, and he chose to call this method rounding up. I would probably have called it the same and, if I had used subtract 0.5 from the input number, I would have probably have called it rounding down.

I don’t know how anyone can say that the function is broken when the input number is minus 4.2 and the result is minus 3 when, in fact, this is the correct result when applied to the equation.

To get any other answer requires an entirely different equation.

gearce
(GRC)

LANG MEY YER LUM REEK
That's ODD ...... In 1911, 3 men were hung for the murder of Sir Edmund Berry at Greenbury Hill, their last names were Green, Berry, and Hill.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th Sep 2007 19:00
You are both right, I actually meant rounding to the nearest integer but my original function was only rounding up. And... -4.2 = -3 was correct for the formula because it wasn't written properly because I hadn't thought about minus numbers.

And you don't apply 0.5 because there is no (easy) way to get -0.5 from a negative number. I found the answer is to double the decimal so the maximum below 0.5 would be 0.499 * 2 = 0.988, which when truncated would still be worth 0; but 0.5 * 2 = 1 ; and 0.999 * 2 = 1.888, truncated = 1
And because you are using the actual number the positive/negative stays the same: -0.6 * 2 = -1.2 truncated = 1

I'm sure this is the best way to do this mathematically.

In programming, nothing exists
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Sep 2007 00:04
Quote: "Multiply by 10, round, then divide by 10."

Huh? Rounding is the whole point of this function.

In programming, nothing exists
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Sep 2007 13:57
That does the same as my original formula, which was proved wrong.

@All
Does anyone know how to do a for loop with a 0.1 increment?

In programming, nothing exists
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Sep 2007 15:44
Is this still being discussed?

@WK,
Since 0.5 can be represented fully and correctly in a float, I can't see where you think the inaccuracy comes from.

Utility plugins collection and
http://www.matrix1.demon.co.uk for older plug-ins and example code
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Sep 2007 16:04
@Window
Oh right, that's probably why my test example looked funny (even though it still worked)
I'm happy with my new function though, thanks for the suggestion

In programming, nothing exists
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 16th Sep 2007 17:08 Edited at: 16th Sep 2007 17:15


would this work?

haven't had a chance to check it yet but as far as my tired brain can think it looks right :p


EDIT: ok just to clarify this finds the nearest integer. It doesn't specifically round up. I thought that was what was wanted, but this thread's too confusing for me, and I can't focus on anything let alone figuring out what was actually intended.

If it was meant to be rounding away from 0, then it would just be


But there are loads of brackets and calculations so I don't know if I'm doing it right.

http://jamesmason01.googlepages.com/index.htm
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 17th Sep 2007 17:18
Why are you guys still trying to come up with formulas?
Is there a problem with mine?


In programming, nothing exists
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 17th Sep 2007 22:04
ok... I missed that one... but I did do a pretty good job of making a longer, less efficient version, you've gotta admit

http://jamesmason01.googlepages.com/index.htm
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Sep 2007 01:06
@Demon
Hey, it's better than what I had to start with.
I just had the realisation that 0.5 isn't important, it's only 0.5 because you double it to make 1. So using the number itself makes it all happen pretty naturally

Mine does actually work though right?

In programming, nothing exists
The Wizard of Aus
17
Years of Service
User Offline
Joined: 14th Sep 2007
Location: The Land Down Under, AUSTRALIA
Posted: 19th Sep 2007 07:43 Edited at: 20th Sep 2007 08:56
Round-toward-nearest

This is perhaps the most intuitive of the various rounding algorithms. In this case, values such as 5.1, 5.2, 5.3, and 5.4 would round down to 5, while values of 5.6, 5.7, 5.8, and 5.9 would round up to 4. The trick, of course, is to decide what to do in the case of the half-way value 5.5. In fact, round-toward-nearest may be considered to be a superset of two complementary options known as round-half-up and round-half-down, each of which treats the 5.5 value in a different manner as discussed below.

Round-Half-Up (Arithmetic Rounding)

This algorithm, which may also be referred to as arithmetic rounding, is the one that we typically associate with the concept of rounding that we learned at school. In this case, a "half-way" value such as 5.5 will round up to 6.

One way to view this is that we can consider there to be ten values that commence with a "5" in the most-significant place (5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, and 5.9). On this basis, it intuitively makes sense for five of the values to round down and for the other five to round up; that is, for the five values 5.0 through 5.4 to round down to 5, and for the remaining five values 5.5 through 5.9 to round up to 6.

It’s worth noting that "half-way" values will always round up when using this algorithm, irrespective of whether the final result is odd or even.

The tricky point with this round-half-up algorithm arrives when we come to consider negative numbers. There is no problem in the case of the values like -5.1, -5.2, -5.3, and -5.4, because these will all round to the nearest integer, which is -5. Similarly, there is no problem in the case of values like -5.6, -5.7, -5.8, and -5.9, because these will all round to -6. The problem arises in the case of "half-way" values like -5.5 and -6.5 and our definition as to what "up" means in the context of "round-half-up." Based on the fact that positive values like +5.5 and +6.5 round up to +6 and +7, respectively, most of us would intuitively expect their negative equivalents of -5.5 and -6.5 to round to -6 and -7, respectively. In this case, we would say that our algorithm was symmetric (with respect to zero) for positive and negative values. (Visualise negative numbers as "increasing" in size toward negative infinity).

However, some applications (and some mathematicians) would regard "up" as referring to positive infinity. Based on this, -5.5 and -6.5 would actually round to -5 and -6, respectively, in which case we would class this as being an asymmetric (with respect to zero) implementation of the round-half-up algorithm.

Round-Half-Down

Perhaps not surprisingly, this incarnation of the Round-Toward-Nearest algorithm acts in the opposite manner to its Round-Half-Up counterpart discussed above. In this case, "half-way" values such as 5.5 and 6.5 will round down to 5 and 6, respectively.

Once again, we run into a problem when we come to consider negative numbers, because what we do with "half-way" values depends on what we understand the term "down" to mean. On the basis that positive values of +5.5 and +6.5 round to +5 and +6, respectively, a symmetric implementation of the round-half-down algorithm will round values of -5.5 and -6.5 to -5 and -6, respectively.

By comparison, in the case of an asymmetric implementation of the algorithm, in which "down" is understood to refer to negative infinity, values of -5.5 and -6.5 will actually be rounded to -6 and -7, respectively.

It should be noted that a symmetric implementation of the round-half-up algorithm acts in the same manner as an asymmetric implementation of the round-half-down algorithm and an asymmetric implementation of the round-half-up algorithm acts in the same manner a symmetric implementation of the round-half-down algorithm.

Having said all that, consider the following:

OBese87’s function (a symmetric implementation of the round-half-up algorithm)



and the same function using an if/else/endif loop (an asymmetric implementation of the round-half-up algorithm)



The Wizard
The Wizard of Aus
17
Years of Service
User Offline
Joined: 14th Sep 2007
Location: The Land Down Under, AUSTRALIA
Posted: 20th Sep 2007 04:24 Edited at: 20th Sep 2007 11:41
Sorry OBese87,



Quote: "Posted: 1st Sep 2007 11:51 Edited: 11th Sep 2007 11:03"


Your edit has taken away the point of argument/discussion.

The wizard
The Wizard of Aus
17
Years of Service
User Offline
Joined: 14th Sep 2007
Location: The Land Down Under, AUSTRALIA
Posted: 6th Oct 2007 07:41
Agreed?

The Wizard

If responsibility was a drug, I wonder how many would be addicted?
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 9th Oct 2007 05:31
I think it is best to round up from 0.5 to 1 and -0.5 to -1
rounding to 0 de-values the number.
banks round down but only to steal money from you

In programming, nothing exists
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 14th Oct 2007 18:50
Quote: " think it is best to round up from 0.5 to 1 and -0.5 to -1"

That's what pretty much everyone does isn't it? Unless you have a specific reason for doing otherwise, then I thought that was the mathematical norm.

http://jamesmason01.googlepages.com/index.htm
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 14th Oct 2007 19:18
Quote: "Unless you have a specific reason for doing otherwise"

Like I said about banks.
Because of interest you might have 48.6 pence in your bank account, the bank will round that to 48p. It might not look like a lot but a few years ago a guy who worked for a bank was jailed because he was depositing 0.1p into his own account every time they did that and he ended up with millions of pounds!

I'm happy with what I have now, it seems pretty fast and is very short.
If anyone can speed it up or shorten it that'd be great

demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 15th Oct 2007 19:52
That's harsh... he was showing originality and it wasn't hurting them :p

wait... if he deposited 0.1p wouldn't his account just keep getting rounded down?

http://jamesmason01.googlepages.com/index.htm
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 16th Oct 2007 23:34
I think the computers only do the rounding at midnight or something.
Don't know the exact details.
good spot though

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 17th Nov 2007 12:22
I've come up with an even faster method! (one line!)


"You must be someone's friend to make comments about them." - MySpace lied.
aluseus GOD
17
Years of Service
User Offline
Joined: 19th Mar 2007
Location: I\'m here. Now I\'m there. I keep moving
Posted: 17th Nov 2007 21:50
so does his code compltely work now? if it doesn't i was going to try to find my own without if.

alus.portbb.com go there.
[quote]A book. I hate books. book is stupid. I know that I need codes but I dont know the codes.[/quote} -zenicanin14 the stupidest user in the world.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Nov 2007 00:46
who?
are you responding to my post in the second person?
Mine doesn't have an if statement, but if you can figure out a faster way to do it that'd be awesome

"You must be someone's friend to make comments about them." - MySpace lied.
aluseus GOD
17
Years of Service
User Offline
Joined: 19th Mar 2007
Location: I\'m here. Now I\'m there. I keep moving
Posted: 22nd Nov 2007 16:51
wait, I just realized something, is the point of this to round up, or round?

alus.portbb.com go there.
Quote: A book. I hate books. book is stupid. I know that I need codes but I dont know the codes -zenicanin14 the stupidest user in the world
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 26th Nov 2007 01:22
well the default on the program is to round down, but there's code for doing both on this post. lots of it.

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 26th Nov 2007 02:24
it rounds to the nearest integer (0.5 = 1)

"You must be someone's friend to make comments about them." - MySpace lied.
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 28th Nov 2007 15:28
@Windowskiller -
Your function does it exactly right. Since .5 can be accurately represented as a float, the version that uses addition and subtraction is better than multiplication and division because the former operations are faster. If 5 and 10 can be represented with floats, .5 and .1 can as well. The number itself is irrelevant in this situation since the decimal can be anywhere.


Come see the WIP!
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Nov 2007 15:32 Edited at: 28th Nov 2007 15:32
@Cash
Really? So
Quote: "
function round(n#)
n = int(n#+n#)-int(n#)
endfunction n"

is faster than
Quote: "
function round(n#)
n = int(n#*2)-int(n#)
endfunction n"


thanks cash

"You must be someone's friend to make comments about them." - MySpace lied.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Nov 2007 15:49
cool
I can understand why now, thinking about it
So how do I get this made into a command then?

"You must be someone's friend to make comments about them." - MySpace lied.
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 28th Nov 2007 18:28
So you were being sarcastic OBese87? That's quite uncalled for. I don't make unqualified statements about coding.


Come see the WIP!
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 28th Nov 2007 18:54
@OBese87

squeeze a little more speed out of it by dumping those int() functions - round is faster than round2



Enjoy your day.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 28th Nov 2007 18:55
huh?
I wasn't being sarcastic

"You must be someone's friend to make comments about them." - MySpace lied.

Login to post a reply

Server time is: 2024-11-25 19:16:49
Your offset time is: 2024-11-25 19:16:49