Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (dr_panther, 7th_zorro), 1,203 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
rounding and var-to-integer conversions #228632
09/21/08 03:38
09/21/08 03:38
Joined: Jun 2008
Posts: 40
pfeldman Offline OP
Newbie
pfeldman  Offline OP
Newbie

Joined: Jun 2008
Posts: 40
When I search the manual, I can find nothing on the following two topics:

(1) rounding: I'd like to be able to round a var to the nearest integer, round down to the nearest integer (floor), or round up to the nearest integer (ceil).

(2) conversion from var to int or from var to short: It would be nice to be able to do such conversions, either via type cast, or by calling a function.

Adding such functions would be a major benefit.

Re: rounding and var-to-integer conversions [Re: pfeldman] #228648
09/21/08 09:19
09/21/08 09:19
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
(1) int round(var x) { return (int)(x + 0.5*sign(x)); }

(2) int i = x; short s = x;


Re: rounding and var-to-integer conversions [Re: Spirit] #228653
09/21/08 10:33
09/21/08 10:33
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
(1) use the integer() function

(2) var <-> int is automatically converted. Forcing a cast will deliver undesired results as the fixpoint value of var won't be converted to int as you'd expect.

Re: rounding and var-to-integer conversions [Re: FBL] #228710
09/21/08 20:09
09/21/08 20:09
Joined: Jun 2008
Posts: 40
pfeldman Offline OP
Newbie
pfeldman  Offline OP
Newbie

Joined: Jun 2008
Posts: 40
OK. Thanks to both of you!

I can't find any mention of the round() function in the online manual.

Re: rounding and var-to-integer conversions [Re: pfeldman] #228719
09/21/08 21:33
09/21/08 21:33
Joined: Jan 2007
Posts: 1,619
Germany
Scorpion Offline
Serious User
Scorpion  Offline
Serious User

Joined: Jan 2007
Posts: 1,619
Germany
It's because there is no round function there. Just use the one Spirit posted

Well and here is the ceil function 4 you wink
Code:
int ceil(var x) { return (int)(x+1); }


Re: rounding and var-to-integer conversions [Re: Scorpion] #228721
09/21/08 22:15
09/21/08 22:15
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
of course there is a round function... As I posted above: integer()

Re: rounding and var-to-integer conversions [Re: FBL] #228751
09/22/08 07:31
09/22/08 07:31
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
No, integer() does not round. It just removes the fractional part of the number. For rounding and ceil use the functions posted by Spirit and Scorpion.


Moderated by  aztec, Spirit 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1