MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: 0.0 is different with 0?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90032] Re: 0.0 is different with 0?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Fri, 27 Jun 2008 06:13:33 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g3vkss$kfs$1@smc.vnet.net>

damayi wrote:

> Here is an example to show my question.
> SF[N_Integer, a_Real, fs_Real] :=
>  Table[Sin[(1 + fs)*k*2 Pi/N + a], {k, 0, N - 1}]
> 
> SF[32, 0.1, 0.0] can show the table contents, however, SF[32, 0.1, 0]
> cannot show the result. Why?

If you were writing this function is a strongly typed language such as 
C/C++, part of the function prototype could be SF(unsigned int n, double 
a, double fs) and when you call the function, the compiler would 
automatically cast (if needed of course) an int into a double or issue a 
warning for possible precision lost.

Now, Mathematica does not have this notion of type. However, it uses 
symbolic expressions and pattern matching. In Mathematica, everything is 
an expression, i.e. something of the form head[elem1, elem2, ...] (a 
head and zero, one, or more elements).

In term of pattern matching, 0.0 and 0 are totally different, since 
their respective head are different:

     Head /@ {0., 0}

     {Real, Integer}

Pattern matching does not cast a head into another head. Since the haead 
of the arguments are

     Head /@ SF[32, 0.1, 0]

     SF[Integer, Real, Integer]

the pattern does not match the pattern given when defining the function 
(the head of the third argument must be Real)

     SF[N_Integer, a_Real, fs_Real]

Rather than using heads, you may want to use explicit tests to check 
whether an argument is numeric or not, as in

     SF[N_Integer, a_Real, fs_?NumericQ]

(Also, the computational model used by Mathematica is not the same when 
you write exact numbers such as 0 (infinite precision) or machine 
precision numbers such as 0.0 (a third model is also available: 
arbitrary precision).)

HTH,
-- Jean-Marc


  • Prev by Date: Re: Re: Happy Birthday Mathematica!
  • Next by Date: Re: Inequality not documented in 6.0
  • Previous by thread: Re: 0.0 is different with 0?
  • Next by thread: Re: 0.0 is different with 0?