Trunc function?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Trunc function?

Gadgetman!
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
Reply | Threaded
Open this post in threaded view
|

Re: Trunc function?

acwest
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
Reply | Threaded
Open this post in threaded view
|

Re: Trunc function?

shadowwynd
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
dpa
Reply | Threaded
Open this post in threaded view
|

Re: Trunc function?

dpa
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 <[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
SNT
Reply | Threaded
Open this post in threaded view
|

Re: Trunc function?

SNT
In reply to this post by Gadgetman!
Hello I use the 'abs' command

best regards Sven

> Am 16.06.2020 um 01:30 schrieb Gadgetman! <[hidden email]>:
>
> 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
I'm using OpenScad Version 2019.05
Reply | Threaded
Open this post in threaded view
|

Re: Trunc function?

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

On Tue, 16 Jun 2020, 07:59 Sven Sylla, <[hidden email]> wrote:
Hello I use the 'abs' command

best regards Sven

> Am 16.06.2020 um 01:30 schrieb Gadgetman! <[hidden email]>:
>
> 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

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org