BIM Coordinator Program (INT) April 22, 2024
Find the next step in your career as a Graphisoft Certified BIM Coordinator!
Libraries & objects
About Archicad and BIMcloud libraries, their management and migration, objects and other library parts, etc.
SOLVED!

Rounding Lengths UP/Down in GDL

Nader Belal
Mentor
Hi every one,

I would like to know if there is a hidden function in GDL to round up or down a length parameter ?

if not, what is the best way to implement it ?

Thanks in advance
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
1 ACCEPTED SOLUTION

Accepted Solutions
Solution
Barry Kelly
Moderator
MOD is the operator you are looking for.

length MOD module

i.e. 2.9 MOD 0.3

This will give you the remaining part of the module (0.2 in this case).

So to round down .... 2.9-(2.9 MOD 0.3)

And to round up ... 2.9-(2.9 MOD 0.3) + 0.3


Or you could do it the long way using INT and FRA
Divide the length by the module and this will give you a decimal number.

INT of that number is the total number of whole modules.
FRA of that number is fractional part of the remaining module length.

If the FRA is greater than 0 then round up by (INT+1) x module length
To round down is simply INT x module length.

Barry.
One of the forum moderators.
Versions 6.5 to 27
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11

View solution in original post

7 REPLIES 7
Solution
Barry Kelly
Moderator
MOD is the operator you are looking for.

length MOD module

i.e. 2.9 MOD 0.3

This will give you the remaining part of the module (0.2 in this case).

So to round down .... 2.9-(2.9 MOD 0.3)

And to round up ... 2.9-(2.9 MOD 0.3) + 0.3


Or you could do it the long way using INT and FRA
Divide the length by the module and this will give you a decimal number.

INT of that number is the total number of whole modules.
FRA of that number is fractional part of the remaining module length.

If the FRA is greater than 0 then round up by (INT+1) x module length
To round down is simply INT x module length.

Barry.
One of the forum moderators.
Versions 6.5 to 27
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11
Nader Belal
Mentor
Thank you Barry, that is extactly what I was searching for
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
Durval
Enthusiast
You should have a look at CEIL (x) and ROUND_INT (x) functions. Depending on what you are trying to round, these can be useful.
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
Durval
Enthusiast
Barry wrote:
And to round up ... 2.9-(2.9 MOD 0.3) + 0.3
You may have problems with this. Suppose you want to round up 2.7. It's already rounded, so the correct value should be 2.7, but your code would generate 3.0 as rounded up value.
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
Barry Kelly
Moderator
Durval wrote:
Barry wrote:
And to round up ... 2.9-(2.9 MOD 0.3) + 0.3
You may have problems with this. Suppose you want to round up 2.7. It's already rounded, so the correct value should be 2.7, but your code would generate 3.0 as rounded up value.

Very true.
Pretty obvious I am not a particularly good coder.
Code first then fix the problems when you don’t get the result you want - that’s the way I tend to work.

Barry.
One of the forum moderators.
Versions 6.5 to 27
Dell XPS- i7-6700 @ 3.4Ghz, 16GB ram, GeForce GTX 960 (2GB), Windows 10
Lenovo Thinkpad - i7-1270P 2.20 GHz, 32GB RAM, Nvidia T550, Windows 11
Nader Belal
Mentor
@Durval thank you, but I have already weight these two solutions and they didn't fit my needs.

I explain:
The problem is that I have a panel with predefined total fixed widths, composed of 2 outer layers and and a frame, and the user have the ability to choose a unified skin thickness from 0mm to 0.5*panel_thickness, with the condition that it must be rounded mm with NO FRACTIONS

So as long the panel thickness forms an even number there will be no problem, ex:
panel 100mm --- forms ---> skin thickness range [0, 50mm]

But if the panel thickness is an odd number:
panel 125 --- forms ---> skin thickness range [0, 62.5mm] !<--- that is what I´m trying to prevent

Let's study your solutions:
CEIL (x)
Returns the smallest integral value that is not smaller than x (always integer). (copy pasted from GDL manual)

panel 125 --- forms ---> skin thickness range [0, ceil(125*0.5)] will get you a range [0, 63]

but 63*2 = 126 mm panel (error - not acceptable)

ROUND_INT (x)
Returns the rounded integer part of x.

in other words, and using the previous example 62.5 mm will be rounded to 63 mm (error - not acceptable)

On the other hand @Barry's solution may have been wrong, but it pointed out where I needed to go, so I modified his solution in a way that it will fulfill my needs, and not prone to errors it the units used in the template is changed by writing this psuedo_code in GDL

values "skin_thickness" , range (0, (0.5*panel_thickness) - (panel_thickness * 0.5 MOD 0.001) )


So thank you all, if it wasn't for you I wouldn't have been able to get this solution
A good friend of mine have once told me that I´m so brute that I´m capable of creating a GDL script capable of creating GDLs.
Durval
Enthusiast
Moonlight wrote:
values "skin_thickness" , range (0, (0.5*panel_thickness) - (panel_thickness * 0.5 MOD 0.001) )
I'm glad you found a solution!
--- www.dtabach.com.br ---
AC 24 BR – MacBook Pro 2,9 GHz Intel Core i7 16GB RAM Mac OS 10.14
Learn and get certified!

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!