discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Trunc function?

G
Gadgetman!
Mon, Jun 15, 2020 11:30 PM

Hi!

Is there a 'trunc' function in OpenSCAD?
(I can't find one)
Or any other simple way to retrieve just the integer part of the result of a
division?

--
Sent from: http://forum.openscad.org/

Hi! Is there a 'trunc' function in OpenSCAD? (I can't find one) Or any other simple way to retrieve just the integer part of the result of a division? -- Sent from: http://forum.openscad.org/
AC
A. Craig West
Mon, Jun 15, 2020 11:35 PM

Openscad has floor, round, and ceil. Depends on which you want

On Mon, 15 Jun 2020, 19:31 Gadgetman!, trygve@totallytrygve.com wrote:

Hi!

Is there a 'trunc' function in OpenSCAD?
(I can't find one)
Or any other simple way to retrieve just the integer part of the result of
a
division?

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Openscad has floor, round, and ceil. Depends on which you want On Mon, 15 Jun 2020, 19:31 Gadgetman!, <trygve@totallytrygve.com> wrote: > Hi! > > Is there a 'trunc' function in OpenSCAD? > (I can't find one) > Or any other simple way to retrieve just the integer part of the result of > a > division? > > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
S
shadowwynd
Tue, Jun 16, 2020 3:48 AM

CEIL rounds up, FLOOR rounds down, ROUND goes to the closet integer .... none
of which is a TRUNC function.  OpenSCAD does not have a trunc function, but
it is easy to make one:

*function trunc(x=0) = ( x > 0 ? floor (x) : ceil(x) );*

Then call it like: echo (trunc(4.3));

Given inputs of:
x = -4.9, -4.5, -4.1, 0.232, 5.1, 5.5, 5.9

TRUNC:  -4, -4, -4, 0, 5, 5, 5

CEIL: -4, -4, -4, 1, 6, 6, 6
ROUND: -5, -5, -4, 0, 5, 6, 6
FLOOR: -5, -5, -5, 0, 5, 5, 5

--
Sent from: http://forum.openscad.org/

CEIL rounds up, FLOOR rounds down, ROUND goes to the closet integer .... none of which is a TRUNC function. OpenSCAD does not have a trunc function, but it is easy to make one: *function trunc(x=0) = ( x > 0 ? floor (x) : ceil(x) );* Then call it like: echo (trunc(4.3)); Given inputs of: x = -4.9, -4.5, -4.1, 0.232, 5.1, 5.5, 5.9 TRUNC: -4, -4, -4, 0, 5, 5, 5 CEIL: -4, -4, -4, 1, 6, 6, 6 ROUND: -5, -5, -4, 0, 5, 6, 6 FLOOR: -5, -5, -5, 0, 5, 5, 5 -- Sent from: http://forum.openscad.org/
D
dpa
Tue, Jun 16, 2020 7:14 AM

or including decimal numbers:

function trunc(x,dec) = x > 0 ? floor(x * pow(10,dec)) / pow(10,dec) :
ceil(x*pow(10,dec)) / pow(10,dec);

echo(trunc(9.875311,2));
echo(trunc(-9.875311,1));

// ECHO: 9.87
// ECHO: -9.8

Am Di., 16. Juni 2020 um 05:49 Uhr schrieb shadowwynd <shadowwynd@gmail.com

:

CEIL rounds up, FLOOR rounds down, ROUND goes to the closet integer ....
none
of which is a TRUNC function.  OpenSCAD does not have a trunc function, but
it is easy to make one:

 *function trunc(x=0) = ( x > 0 ? floor (x) : ceil(x) );*

Then call it like: echo (trunc(4.3));

Given inputs of:
x = -4.9, -4.5, -4.1, 0.232, 5.1, 5.5, 5.9

TRUNC:  -4, -4, -4, 0, 5, 5, 5

CEIL: -4, -4, -4, 1, 6, 6, 6
ROUND: -5, -5, -4, 0, 5, 6, 6
FLOOR: -5, -5, -5, 0, 5, 5, 5

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

or including decimal numbers: function trunc(x,dec) = x > 0 ? floor(x * pow(10,dec)) / pow(10,dec) : ceil(x*pow(10,dec)) / pow(10,dec); echo(trunc(9.875311,2)); echo(trunc(-9.875311,1)); // ECHO: 9.87 // ECHO: -9.8 Am Di., 16. Juni 2020 um 05:49 Uhr schrieb shadowwynd <shadowwynd@gmail.com >: > CEIL rounds up, FLOOR rounds down, ROUND goes to the closet integer .... > none > of which is a TRUNC function. OpenSCAD does not have a trunc function, but > it is easy to make one: > > *function trunc(x=0) = ( x > 0 ? floor (x) : ceil(x) );* > > Then call it like: echo (trunc(4.3)); > > Given inputs of: > x = -4.9, -4.5, -4.1, 0.232, 5.1, 5.5, 5.9 > > TRUNC: -4, -4, -4, 0, 5, 5, 5 > > CEIL: -4, -4, -4, 1, 6, 6, 6 > ROUND: -5, -5, -4, 0, 5, 6, 6 > FLOOR: -5, -5, -5, 0, 5, 5, 5 > > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
SS
Sven Sylla
Tue, Jun 16, 2020 11:56 AM

Hello I use the 'abs' command

best regards Sven

Am 16.06.2020 um 01:30 schrieb Gadgetman! trygve@totallytrygve.com:

Hi!

Is there a 'trunc' function in OpenSCAD?
(I can't find one)
Or any other simple way to retrieve just the integer part of the result of a
division?

--
Sent from: http://forum.openscad.org/


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Hello I use the 'abs' command best regards Sven > Am 16.06.2020 um 01:30 schrieb Gadgetman! <trygve@totallytrygve.com>: > > Hi! > > Is there a 'trunc' function in OpenSCAD? > (I can't find one) > Or any other simple way to retrieve just the integer part of the result of a > division? > > > > > > -- > Sent from: http://forum.openscad.org/ > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
AC
A. Craig West
Tue, Jun 16, 2020 12:08 PM

Something like:
function trunc(x) = sign(x) *floor(abs(x)) ;

On Tue, 16 Jun 2020, 07:59 Sven Sylla, sven.sylla@freenet.de wrote:

Hello I use the 'abs' command

best regards Sven

Am 16.06.2020 um 01:30 schrieb Gadgetman! trygve@totallytrygve.com:

Hi!

Is there a 'trunc' function in OpenSCAD?
(I can't find one)
Or any other simple way to retrieve just the integer part of the result

of a

Something like: function trunc(x) = sign(x) *floor(abs(x)) ; On Tue, 16 Jun 2020, 07:59 Sven Sylla, <sven.sylla@freenet.de> wrote: > Hello I use the 'abs' command > > best regards Sven > > > Am 16.06.2020 um 01:30 schrieb Gadgetman! <trygve@totallytrygve.com>: > > > > Hi! > > > > Is there a 'trunc' function in OpenSCAD? > > (I can't find one) > > Or any other simple way to retrieve just the integer part of the result > of a > > division? > > > > > > > > > > > > -- > > Sent from: http://forum.openscad.org/ > > > > _______________________________________________ > > OpenSCAD mailing list > > Discuss@lists.openscad.org > > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >