# Trunc function?

6 messages
Open this post in threaded view
|

## Trunc function?

 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 [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Trunc function?

 Openscad has floor, round, and ceil. Depends on which you want On Mon, 15 Jun 2020, 19:31 Gadgetman!, <[hidden email]> 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 [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Open this post in threaded view
|

## Re: Trunc function?

 This post was updated on . In reply to this post by Gadgetman! 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
Open this post in threaded view
|

## Re: Trunc function?

 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.8Am Di., 16. Juni 2020 um 05:49 Uhr schrieb shadowwynd <[hidden email]>: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 [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org