ATTENTION

This FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices.


flexsim.com

Go Back   FlexSim Community Forum > FlexSim Software > Q&A
Downloads

Q&A Using Flexsim and building models

  #1  
Old 09-25-2008
Jörg Vogel's Avatar
Jörg Vogel Jörg Vogel is offline
Flexsim User
 
Join Date: Sep 2007
Location: Hannover, Germany
Posts: 643
Downloads: 35
Uploads: 0
Thanks: 802
Thanked 665 Times in 410 Posts
Rep Power: 642
Jörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond reputeJörg Vogel has a reputation beyond repute
Default C++ function "ceil" in Flexscript

Hi,

for the c++ function "floor" I can use trunc so far, but for "ceil" I haven't found a similar function in flexscript.

I built two variants:

// Ceil Function
// ceil(4/3) = 2
int result;
double g = 4;
double h = 3;
// Variant 1

result = g/h;
if (fmod(g,h))
{
pt("rest: ");pf(fmod(g,h));pr();
result = trunc(g/h)+1;
}

// Variant 2
double temp1=g/h;
double temp2=temp1+0.9999999;
result=maxof(trunc(temp1),trunc((temp2)));
//----
//Variants End
//----
pt("result: ");pd(result);pr();

Are there any shorter and faster ways to build a ceil-function in flexscript?

Have some fun with this quest.

Jörg
  #2  
Old 09-26-2008
Dirk-Jan Dirk-Jan is offline
Talumis, Flexsim Distributor, The Netherlands
 
Join Date: Aug 2007
Location: Utrecht, Netherlands
Posts: 43
Downloads: 35
Uploads: 0
Thanks: 11
Thanked 22 Times in 16 Posts
Rep Power: 170
Dirk-Jan will become famous soon enough
Default

Jorg,

I have aways used:

// Variant 3

int result;
double g = 4;
double h = 3;
result = round((g/h) +0.5);
return result;



Rgds,
DJ
The Following User Says Thank You to Dirk-Jan For This Useful Post:
Jörg Vogel (09-26-2008)
  #3  
Old 09-26-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

My version:
Code:
double n = 3.2;
//double n = 3.0;
int res;
if(frac(n)==0) res = n;
else res = trunc(n+1);
pd(res);pr();
When I check Dirk-Jan's version, it is a surprise that his formula gives me correct result when g/h is an integer. Then I found Flexsim script has a different understanding of rounding .5 fraction as we typically do. Shouldn't round(1.5) return 2 instead of 1 as the Flexsim script do?

When doing rounding, a commonly used rule is called Round-to-Even method (see http://en.wikipedia.org/wiki/Roundin...to-even_method). So
round(0.5) = 0
round(1.5) = 2
round(2.5) = 2
round(3.5) = 4
round(4.5) = 4
etc.

If Flexsim script follows the Round-to-Even rule (which I think it should), Dirk-Jan's version won't give you correct answer.
__________________
Best,
Alan
  #4  
Old 10-01-2008
Dirk-Jan Dirk-Jan is offline
Talumis, Flexsim Distributor, The Netherlands
 
Join Date: Aug 2007
Location: Utrecht, Netherlands
Posts: 43
Downloads: 35
Uploads: 0
Thanks: 11
Thanked 22 Times in 16 Posts
Rep Power: 170
Dirk-Jan will become famous soon enough
Default

Alan,

I do not think a round to even function is that common.

Rounding in my opinion is done to the nearest integer. I would have thought that round(1.5) would result in 2 rather than 1 (as Flexscript does). But I am not sure Flexscript will be changed to calculate it like that. And if they do I need to change this solution :-)


DJ
  #5  
Old 10-01-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

DJ,

Round-to-even is mainly for statistical purpose so that you won't get a biased result when you always round up. The reason is that statistically .5 is not closer to either the immediately integer number larger than it or the one smaller than it. So it should get half of chance to round up, and half of chance to round down. This may not be that significant from a practical standpoint. But since Flexsim is a simulation software and statistics is the main thing that we should care when doing simulation, we may need "right" statistics here. And it is not hard to do, right? :-)

Flexscript will always round down. That is,
round(0.5) = 0
round(1.5) = 1
round(2.5) = 2
...
This is definitely different from what we typically do (either round up or round-to-even).

Alan
__________________
Best,
Alan
The Following User Says Thank You to AlanZhang For This Useful Post:
Jörg Vogel (10-02-2008)


Thread Thread Starter Forum Replies Last Post
Is it possible one object comprised of the function of "combiner" and "separetor"? Vic Li Q&A 1 08-19-2008 04:41 AM
error message "Clock overflow, running stopped" Martin Kooijman Q&A 11 04-17-2008 10:29 AM
about "no select" and "show parameter window from side bar" qin tian Gripes and Goodies 3 03-21-2008 08:10 AM
Which variable stores "Properties -> General ->Flags -> Protected" information? KelvinHo Q&A 1 03-06-2008 06:18 AM
"Getting Started" and "Tutorial" models for v4.01 Cliff King Product Announcements 0 12-10-2007 07:34 PM


All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.