discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Determining what data type a variable is holding

R
runsun
Mon, Feb 15, 2016 5:57 AM

MichaelAtOz wrote

@runsun, ' isint(int(x))' I presume your int() function returns undef or
nan when x is not a number?

Not exactly. It will try to type-cast x into an int. So int("3.3")=> 3. The
test cases for int() are:

int ( x )=int ( tested:22 ) (mode:12)
| 0> int(3)= 3
| 1> int(3.3)= 3
| 2> int(-3.3)= -3
| 3> int(3.8)= 4
| 4> int(9.8)= 10
| 5> int(-3.8)= -4
| 6> int("-3.3")= -3
| 7> int("-3.8")= -3 // !!Note this!!
| 8> int("3")= 3
| 9> int("3.0")= 3
| 10> int("0.3")= 0
| 11> int("34")= 34
| 12> int("-34")= -34
| 13> int("-34.5")= -34
| 14> int("345")= 345
| 15> int("3456")= 3456
| 16> int("789987")= 789987
| 17> int("789.987")= 789 // !!Note this!!
| 18> int([3,4])= undef
| 19> int("x")= undef
| 20> int("34-5")= undef
| 21> int("34x56")= undef

I apologize that I didn't realize there are couple of user functions used. I
double checked and see that it'd take too much work trying to put all of
them here ('cos they also use other user functions). So maybe just remove
the "range detection" part ? I suppose not many ppl will need it, anyway.


$  Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 );  $ tips: hash( 1 , 2 ), sweep( 1 , 2 ), var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control , type

--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16147.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

MichaelAtOz wrote > @runsun, ' isint(int(x))' I presume your int() function returns undef or > nan when x is not a number? Not exactly. It will try to type-cast x into an int. So int("3.3")=> 3. The test cases for int() are: > int ( x )=int ( tested:22 ) (mode:12) > | 0> int(3)= 3 > | 1> int(3.3)= 3 > | 2> int(-3.3)= -3 > | 3> int(3.8)= 4 > | 4> int(9.8)= 10 > | 5> int(-3.8)= -4 > | 6> int("-3.3")= -3 > | 7> int("-3.8")= -3 // !!Note this!! > | 8> int("3")= 3 > | 9> int("3.0")= 3 > | 10> int("0.3")= 0 > | 11> int("34")= 34 > | 12> int("-34")= -34 > | 13> int("-34.5")= -34 > | 14> int("345")= 345 > | 15> int("3456")= 3456 > | 16> int("789987")= 789987 > | 17> int("789.987")= 789 // !!Note this!! > | 18> int([3,4])= undef > | 19> int("x")= undef > | 20> int("34-5")= undef > | 21> int("34x56")= undef I apologize that I didn't realize there are couple of user functions used. I double checked and see that it'd take too much work trying to put all of them here ('cos they also use other user functions). So maybe just remove the "range detection" part ? I suppose not many ppl will need it, anyway. ----- $ Runsun Pan, PhD $ libs: doctest , faces ( git ), offline doc ( git ), runscad.py( 1 , 2 , git ), synwrite( 1 , 2 ); $ tips: hash( 1 , 2 ), sweep( 1 , 2 ), var( 1 , 2 ), lerp , animGif , prodVid , precision( 1 , 2 ), xl-control , type -- View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16147.html Sent from the OpenSCAD mailing list archive at Nabble.com.
T
Trygon
Mon, Feb 15, 2016 2:39 PM

Hi Runsun,

I had not thought of str(x)==x to test for a string, thanks.

Trygon

--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16148.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi Runsun, I had not thought of str(x)==x to test for a string, thanks. Trygon -- View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16148.html Sent from the OpenSCAD mailing list archive at Nabble.com.
T
Trygon
Mon, Feb 15, 2016 2:46 PM

Hi Doug,

You have my vote - type tests of the form is_int etc. would be nice to have
since they would be guaranteed to be robust.

-Trygon

--
View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16149.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi Doug, You have my vote - type tests of the form is_int etc. would be nice to have since they would be guaranteed to be robust. -Trygon -- View this message in context: http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16149.html Sent from the OpenSCAD mailing list archive at Nabble.com.
DM
doug moen
Wed, Feb 17, 2016 3:37 PM

Added issue #1584, Type Test Functions.
https://github.com/openscad/openscad/issues/1584

On 15 February 2016 at 09:46, Trygon db5765@outlook.com wrote:

Hi Doug,

You have my vote - type tests of the form is_int etc. would be nice to have
since they would be guaranteed to be robust.

-Trygon

--
View this message in context:
http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16149.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


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

Added issue #1584, Type Test Functions. https://github.com/openscad/openscad/issues/1584 On 15 February 2016 at 09:46, Trygon <db5765@outlook.com> wrote: > Hi Doug, > > You have my vote - type tests of the form is_int etc. would be nice to have > since they would be guaranteed to be robust. > > -Trygon > > > > -- > View this message in context: > http://forum.openscad.org/Determining-what-data-type-a-variable-is-holding-tp16111p16149.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > >