11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10 • Chapter 2: Procedures, Variables, and Extending Maple122107060112000000000The procedure f returns fractions rather than floating-point numbersso you must compose it with evalf; that is, use evalf(f()). Alternatively,you can perform this operation by using Maple’s composition operator,@.> (evalf @ f)();6.648630719The following uniform procedure uses evalf to evaluate the constantsin the range specification, r, to floating-point numbers, the map commandto multiply both endpoints of the range by 10^Digits, and round toround the results to integers.> uniform := proc( r::constant..constant )> local intrange, f;> intrange := map( x -> round(x*10^Digits), evalf(r) );> f := rand( intrange );> (evalf @ eval(f)) / 10^Digits;> end proc:You can now generate random floating-point numbers between 4and 7.> U := uniform(4..7):> seq( U(), i=1..20 );4.559076346, 4.939267370, 5.542851096, 4.260060897,4.976009937, 5.598293374, 4.547350944,5.647078832, 5.133877918, 5.249590037,4.120953928, 6.836344299, 5.374608653,4.586266491, 5.481365622, 5.384244382,5.190575456, 5.207535837, 5.553710879,4.163815544The uniform procedure has a serious flaw: uniform uses the currentvalue of Digits to construct intrange; thus, U depends on the value ofDigits when uniform creates it. On the other hand, the evalf commandwithin U uses the value of Digits that is current when you invoke U. Thesetwo values are not always identical.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!