ATTENTIONThis 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 |
#1
|
||||
|
||||
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
|
|||
|
|||
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
|
||||
|
||||
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 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
|
|||
|
|||
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
|
||||
|
||||
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 |