Re: IsIntegerOrFloat
- To: mathgroup at smc.vnet.net
- Subject: [mg87037] Re: IsIntegerOrFloat
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Sun, 30 Mar 2008 01:14:11 -0500 (EST)
- References: <fsl214$g77$1@smc.vnet.net>
carlos at colorado.edu schrieb: > Thanks to all who replied to my previous questions. Those functions work > fine now. Here is another question - I apologize if it has been asked before. > I need a function > > IsIntegerOrFloat[expr] > > that returns True if expr, which is generally a flat list, contains > only integers or floating point numbers, and False otherwise. > Rational numbers such as 1/2 are considered symbolic for this test; so is > \[Pi] or Sqrt[5]. Appearance of any symbol, as in 2.0*a+3.5, makes it False. > As in previous questions, this should work on versions >=4.0. > > Use: unified printing of simulation data structures in table formats. > Expressions containing only integers or floats are filtered through > PaddedForm[] and ToString[], others through Symbol[]. The end > result (a flat table of strings) is filtered through TableForm with > appropriate options. > Hi Carlos this seems to work: IsIntegerOrFloat[list_] := With[{ll = Flatten@list}, If[Length[ll] == Length[Cases[ll, _Real | _Integer]], True, False]] l1={1, 2.0, \[Pi], "a", b, Sqrt[2]} l2 = Union[RandomInteger[{1, 10}, {5}], RandomReal[{1, 10}, {5}]] l3 = {1, {2.1, {1.6, 2, 5}}, 6} l4 = {1, {2.1, {1.6, Pi, 5}}, 6} IsIntegerOrFloat[l1] ---> False IsIntegerOrFloat[l2] ---> True IsIntegerOrFloat[l3] ---> True IsIntegerOrFloat[l4] ---> False Gruss Peter -- ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de