Archive for April, 2007

512Part IIIAdvanced Features and TechniquesTable 27-5(continued) FunctionBehaviorbcpow()Raises the (Web hosting reviews)

Wednesday, April 25th, 2007

512Part IIIAdvanced Features and TechniquesTable 27-5(continued) FunctionBehaviorbcpow()Raises the first argument to the power of the second argument. The number ofdecimal places in the result is set by the scale factor if supplied. bcsqrt()Returns the square root of its argument, with number of decimal places set by theoptional scale factor. bcscale()Sets the default scale factor for subsequent BC function calls. An arbitrary-precision exampleHere s an example of using the arbitrary-precision functions for exact integer arithmetic. Thefollowing code: for ($x = 1; $x < 25; $x++) { print( $x raised to the power of $x is . bcpow($x, $x) .
); } will print like this: 1 raised to the power of 1 is 12 raised to the power of 2 is 43 raised to the power of 3 is 274 raised to the power of 4 is 2565 raised to the power of 5 is 31256 raised to the power of 6 is 466567 raised to the power of 7 is 8235438 raised to the power of 8 is 167772169 raised to the power of 9 is 38742048910 raised to the power of 10 is 1000000000011 raised to the power of 11 is 28531167061112 raised to the power of 12 is 891610044825613 raised to the power of 13 is 30287510659225314 raised to the power of 14 is 1111200682555801615 raised to the power of 15 is 43789389038085937516 raised to the power of 16 is 1844674407370955161617 raised to the power of 17 is 82724026188633676417718 raised to the power of 18 is 3934640807529653757542419 raised to the power of 19 is 197841965566031358912397920 raised to the power of 20 is 10485760000000000000000000021 raised to the power of 21 is 584258701838598252138112442122 raised to the power of 22 is 34142787736421955739664672358423 raised to the power of 23 is 2088046799984791203435503291056724 raised to the power of 24 is 133373577685028412444908147284377625 raised to the power of 25 is 88817841970012523233890533447265625If we had used the regular PHP integer type for this computation, the integers would haveoverflowedwell before the end, and the rest of the loop would have been calculated inapproximate floating point.
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

511Chapter 27MathematicsArbitrary Precision (BC) The integer (Web hosting support) and double

Wednesday, April 25th, 2007

511Chapter 27MathematicsArbitrary Precision (BC) The integer and double types are fine for most of the mathematical tasks that arise in Webscripting, but each instance of these types is stored in a fixed amount of computer memory, and so the size and precision of the numbers these types can represent is inherently limited. Although the exact range of these types may depend on the architecture of your servermachine, integers typically range from 231 1 to 231 1, and doubles can represent about 13to 14 decimal digits of precision. For tasks that require greater range or precision, PHP offersthe arbitrary-precisionmath functions (also known as BC functions, from the name of the Unix- based, arbitrary-precision calculating utility). Especially if you compiled PHP yourself, the arbitrary-precision functions may not have beenincluded in the compilation you need to have included the flag –enable-bcmathat con- figuration time. To check whether the functions are present, try evaluating bcadd( 1 , 1 ) if you get an unbound function error, you will have to reconfigure and recompile PHP. Instead of using the fixed-length numerical types, the BC functions have strings as argumentsand return values. Because strings in PHP are limited only by available memory, numbers canbe as long as you like. The underlying computations are performed in decimal and are donemuch as you would do them with pen and paper (if you were very fast and very patient). When operating with integers, the BC functions are exact and use as many digits as needed; when operating with floating-point numbers, computations are done to as many decimalplaces as you specify. The BC functions are summarized in Table 27-5. Most of the functions take an optional scale factor (an integer) as a final argument, whichdetermines how many decimal places will be in the result. If such an argument is not sup- plied, the scale is the default scale, which, in turn, can be set by calling bcscale(). Thedefault for the default value (that is, if bcscale()has never been called) can also be set inthe initialization file php.ini. Table 27-5: Arbitrary-Precision (BC) Math FunctionsFunctionBehaviorbcadd()Takes two string arguments representing numbers, and an optional integer scaleparameter. Returns the sum of the first two arguments as a string, with the numberof decimal places in the result determined by the scale parameter. If no scaleparameter is supplied, the default scale is used (which is settable by bcscale()). bcsub()Similar to bcadd(), except that it returns the subtraction of the second argumentfrom the first. bcmul()Similar to bcadd()but returns the product of its arguments. bcdiv()Similar to bcadd()but returns the result of dividing the first argument by thesecond. bcmod()Returns the modulus (remainder) of the first argument as divided by the second. Because the return type is integral, no scale argument is taken. ContinuedNote31
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

510Part IIIAdvanced Features and TechniquesFigure 27-1:Trigonometric function examplesTrigonometry (Top ten web hosting)

Tuesday, April 24th, 2007

510Part IIIAdvanced Features and TechniquesFigure 27-1:Trigonometric function examplesTrigonometry in One ParagraphImagine a circle with a radius of 1, centered at 0,0 in the coordinate plane. Start at the right-handedge (at position (1,0)), and trace a certain distance along the circle counterclockwise. For exam- ple, a distance of 2 pi would take you once around the circle and back to your starting point. Clockwise travel counts as a negative distance. For any such distance, the sine function tells youthe y-value of the coordinate you arrive at, the cosine function tells you the x-value of that coor- dinate, and the tangent function is a ratio of the two, from which you can infer the slope of theline tangent to the circle at that point. The functions arccosine, arcsine, and arctangent are insome sense inverses of their corresponding functions they map back from an x, y, or y/x ratioto the distance of a circular trip that would arrive at that x-coordinate, y-coordinate, or ratiothereof. Because adding a multiple of 2 pi to any distance brings you around to the same pointagain, these inverse functions might have an infinite number of answers per input, making themill-defined instead, they are restricted to a range corresponding to one particular trip aroundhalf of the circle and so have well-defined results.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

509Chapter 27Mathematics$func_name = $func_array[$y]; $input = $input_array[$x]; print( ); (Frontpage web hosting)

Tuesday, April 24th, 2007

509Chapter 27Mathematics$func_name = $func_array[$y]; $input = $input_array[$x]; print( ); printf( %4.4f , $func_name($input)); print( ); } print( ); } print( ); } ?> Trigonometric Function Examples Trigonometric function examples ); display_func_results(array( sin , cos , tan ), array(-1.25 * pi(), -1.0 * pi(), -0.75 * pi(), -0.5 * pi(), -0.25 * pi(), 0,0.25 * pi(), 0.5 * pi(), 0.75 * pi(), pi(), 1.25 * pi())); ?> The display_func_results()function of Listing 27-1 uses several tricks we ve seen inprevious chapters: using a string variable as the name of a function to call (covered near theend of Chapter 26) and using the string concatenation operator (.) to pull together a printstring in the middle of a print statement (covered in Chapter 8) . Figure 27-1 shows the basic trigonometric functions over an input range of -5/4pito 5/4piand the basic inverse trigonometric function over inputs from -1.0to 1.0. The very largetangent values are due to denominators that should theoretically be zero but instead differslightly from zero due to rounding error. Cross- Reference31
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

508Part IIIAdvanced Features and TechniquesTable 27-4(continued) FunctionBehaviorAcos()Takes a (Web site layout)

Tuesday, April 24th, 2007

508Part IIIAdvanced Features and TechniquesTable 27-4(continued) FunctionBehaviorAcos()Takes a numerical argument and returns the arccosine of the argument in radians. Inputs must be between 1.0 and 1.0 [inputs outside that range will return a result ofNAN(for not a number )]. Results are in the range 0to pi. Atan()Takes a numerical argument and returns the arctangent of the argument in radians. Results are in the range pi/2to pi/2. Atan2()A variant of atan()that takes two arguments. Atan($y, $x)is identical toatan($y/$x)when $xis positive, but the quadrant of atan2 s result depends onthe signs of both $yand $x. Range of the result is from pito pi. Rather than writing down a table of sample function results, let s resort to our usual trick ofwriting code that automatically displays examples as an HTML table. Listing 27-1 shows botha generalized function for displaying a set of one-argument functions applied to a set ofnumerical arguments and then the result of using this display function to make trigonometricexample tables. The results are displayed in Figure 27-1. Listing 27-1:Displaying trigonometric function resultsINPUT\FUNCTION ); for($y = 0; $y < count($func_array); $y++) print( $func_array[$y] ); print( ); /* print the rest of the table */ for($x = 0; $x < count($input_array); $x++) { /* print column entries for inputs */ print( . sprintf( %.4f , $input_array[$x]) . ); for($y = 0; $y < count($func_array); $y++) {
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Web hosting account - 507Chapter 27MathematicsTable 27-3: Exponential FunctionsFunctionBehaviorpow()Takes two numerical arguments

Tuesday, April 24th, 2007

507Chapter 27MathematicsTable 27-3: Exponential FunctionsFunctionBehaviorpow()Takes two numerical arguments and returns the first argument raised to the power ofthe second. The value of pow($x, $y)is xy. exp()Takes a single argument and raises eto that power. The value of exp($x)is ex. log()The natural log function. Takes a single argument and returns its base e logarithm. If ey= x, then the value of log($x)is y. log10()Takes a single argument and returns its base-10 logarithm. If 10y= x, then the valueof log10($x)is y. TrigonometryAlthough explaining the math behind the PHP functions in this chapter is beyond the scopeof this book, we ve made an exception just this once. (See the sidebar Trigonometry in OneParagraph. Anyone who doesn t already know trigonometry will, of course, find the sidebarcompletely impenetrable, but we hope that those who know trig will at least be amused byhow short it is.) PHP offers the standard set of basic trigonometric functions as well as the constant M_PI, anapproximation of pi as a double that prints as 3.1415926535898. This constant can be usedanywhere you would use the literal number itself, and it is also interchangeable with the pi() function. (For other constants derived from pi, see the Mathematical Constants section atthe beginning of this chapter.) Both of the following statements have the same result: $my_pi = M_PI; $my_pi = pi(); The basic trig functions are summarized in Table 27-4. Table 27-4: Trigonometric FunctionsFunctionBehaviorpi()Takes no arguments and returns an approximation of pi (3.1415926535898). Can beused interchangeably with the constant M_PI. Sin()Takes a numerical argument in radians and returns the sine of the argument as a double. Cos()Takes a numerical argument in radians and returns the cosine of the argument as adouble. Tan()Takes a numerical argument in radians and returns the tangent of the argument as adouble. Asin()Takes a numerical argument and returns the arcsine of the argument in radians. Inputsmust be between 1.0 and 1.0 [inputs outside that range will return a result of NAN(for not a number )]. Results are in the range pi/2to pi/2. Continued31
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

506Part IIIAdvanced Features and TechniquesDon t confuse the read (Web host server)

Monday, April 23rd, 2007

506Part IIIAdvanced Features and TechniquesDon t confuse the read formats of numbers with their representations as strings for the pur- poses of base conversion. For example, although 10 in base 16 is the number 16 in base 10, the expression HexDec(0×10)evaluates to the string 22 . Why? There are really threeconversions happening: when 0×10is read (converts from hex to internal binary), when theargument is auto-converted (from internal binary number to the decimal string 16 ), andin the operation of the function (from assumed base 16 to decimal 22 ). If you want justone conversion, the desired expression is HexDec( 10 ). The base conversion functions expect their string arguments to be integers, not floating-pointnumbers. That means you can t use these functions to convert a binary 10.1 to a decimal 2.5. Exponents and LogarithmsPHP includes the standard exponential and logarithmic functions, in both base 10 and base evarieties (shown in Table 27-3). Unlike with exp()and the base e, there is no single-argument function to raise 10 to a givenpower, but in its place you can use the two-argument function pow()with 10as the first argument. You can verify that exponential and power functions of the same base are inverses of eachother, by testing an identity like this: $test_449 = 449.0; $test_449 = pow(10, exp(log(log10($test_449)))); print( test_449 is $test_449
); which gives the browser output: test_449 is 449NoteCautionContinuedAlthough the capitalized portions (including the PHP_FUNCTIONdeclaration) are macros-specificto the PHP framework, much of the body of this code is straight C. The code might look denseand confusing at first, but most of the action has to do with PHP s special treatment of types. Here is roughly what is happening, in order: 1.The arguments that ceil()was called with are retrieved and counted if the count isanything other than 1, the function call returns with an error. 2.The single argument is converted to a number if it is a scalar type other than a number this handles the possibility of string arguments, as in ceil( 5.4 ). 3.Now the numerical argument is tested to see whether it is a PHP long (aka integer) or aPHP double. If it turns out to be a long, the value as a long is returned. 4.The interesting case is if the value is a PHP double. If so, the C double value is extracted, it is run through the C function ceil, the result is converted to a C long; then that value iswrapped up and returned as a PHP long. In other words, the PHP implementation of ceilis simply the C function ceil, wrapped up in alot of type conversion and argument checking. This is the case with many of PHP s functions thathave exact analogues in C.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

505Chapter 27Mathematics1jj in base 20 is 1jj in (Kids web site)

Monday, April 23rd, 2007

505Chapter 27Mathematics1jj in base 20 is 1jj in base 201jj in base 20 is 1h1 in base 211jj in base 20 is 1e7 in base 221jj in base 20 is 1bh in base 231jj in base 20 is 197 in base 241jj in base 20 is 16o in base 251jj in base 20 is 14j in base 261jj in base 20 is 12g in base 271jj in base 20 is 10f in base 281jj in base 20 is rg in base 291jj in base 20 is qj in base 301jj in base 20 is po in base 311jj in base 20 is ov in base 321jj in base 20 is o7 in base 331jj in base 20 is nh in base 341jj in base 20 is mt in base 351jj in base 20 is m7 in base 36Notice that although all the base-conversion functions take string arguments and returnstring values, you can use decimal numerical arguments and rely on PHP s type conversion(but see the cautionary note that follows). In other words, both DecBin( 1234 )andDecBin(1234)will yield the same result. A Glimpse behind the CurtainHow are built-in PHP functions really implemented? This is likely to be of interest only to C pro- grammers and/or those who care about the inner workings of PHP, but we thought that it mightbe revealing to see why so many PHP functions work just like their C counterparts. What follows is the actual implementation for the PHP function ceil, which is intended to con- vert a double to the smallest integer that is greater than or equal to it. PHP_FUNCTION(ceil) { zval **value; if (ARG_COUNT(ht)!=1||getParametersEx(1,&value)==FAILURE) { WRONG_PARAM_COUNT; } convert_scalar_to_number_ex(value); if ((*value)->type == IS_DOUBLE) { RETURN_LONG((long)ceil((*value)->value.dval)); } else if ((*value)->type == IS_LONG) { RETURN_LONG((*value)->value.lval); } RETURN_FALSE; } Continued31
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services

Unlimited web hosting - 504Part IIIAdvanced Features and TechniquesTable 27-2(continued) FunctionBehaviorHexDec()Like BinDec(),

Monday, April 23rd, 2007

504Part IIIAdvanced Features and TechniquesTable 27-2(continued) FunctionBehaviorHexDec()Like BinDec(), but converts from base 16 to base 10. DecHex()Like BinDec(), but converts from base 10 to base 16. baseconvert()Takes a string argument (the integer to be converted) and two integerarguments (the original base, and the desired base). Returns a stringrepresenting the converted number digits higher than 9 (from 10 to 35) are represented by the letters a z. Both the original and desired bases mustbe in the range 2 36. All the base-conversion functions are special-purpose, converting from one particular base toanother, except for base_convert(), which accepts an arbitrary start base and destinationbase. Here s an example of base_convert()in action: function display_bases($start_string, $start_base) { for ($new_base = 2; $new_base <= 36; $new_base++) { $converted = base_convert($start_string, $start_base, $new_base); print( $start_string in base $start_baseis $converted in base $new_base
); } } display_bases( 1jj , 20); This code yields the browser output: 1jj in base 20 is 1100011111 in base 21jj in base 20 is 1002121 in base 31jj in base 20 is 30133 in base 41jj in base 20 is 11144 in base 51jj in base 20 is 3411 in base 61jj in base 20 is 2221 in base 71jj in base 20 is 1437 in base 81jj in base 20 is 1077 in base 91jj in base 20 is 799 in base 101jj in base 20 is 667 in base 111jj in base 20 is 567 in base 121jj in base 20 is 496 in base 131jj in base 20 is 411 in base 141jj in base 20 is 384 in base 151jj in base 20 is 31f in base 161jj in base 20 is 2d0 in base 171jj in base 20 is 287 in base 181jj in base 20 is 241 in base 1931
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

503Chapter 27MathematicsThey simply test for a particular numeric (Yahoo web space)

Sunday, April 22nd, 2007

503Chapter 27MathematicsThey simply test for a particular numeric type. Here are more usage examples and what youmight expect from each: is_int(4) // Trueis_int(4.2) // False, it s a float or doubleis_int( 4 ) // False, this test is stricter than is numericis_int(4 * 2) // True, this expression yields an integerYou might also occasionally see the test is_long(). This test simply maps to the is_int() function. The other numeric type in PHP can be tested for using is_float(), which is aliasedby is_double(). Again, usage is not exactly tricky or even complex. is_float(4) // False, but you knew that alreadyis_float(4.212) // True, but you knew that as wellis_float(4 / 3) // Trueis_float(M_PI) // True, maybe you knew that, maybe you didn tTwo more tests are slightly more obscure: is_finite()and is_infinite()test for exactlywhat their names suggest, although strictly speaking, their range is governed not by actualinfinity (how would you test that?) but by the boundaries of the float value that your systemallows. Finally, we have is_nan(), which we covered in Chapter 10. You might be tempted to useis_nan()to test for any non-numeric value. You ll be unpleasantly surprised if you rely onthis functionality. The more appropriate use of this function is to test for an unreasonable orimprobable mathematical expression such as acos(2). Base ConversionThe default base in PHP for reading in or printing out numbers is 10. In addition, you caninstruct PHP to read octal numbers in base 8 (by starting the number with a leading 0) orhexadecimal numbers in base 16 (by starting the number with a 0x). For more on read formats of numbers, including octal and hexadecimal notation, seeChapter 5. Once numbers are read in, of course, they are represented in binary format in memory, andall the basic arithmetic and mathematical calculations are carried out internally in base 2. PHP also has a number of functions for translating between different bases, which are sum- marized in Table 27-2. Table 27-2: Base Conversion FunctionsFunctionBehaviorBinDec()Takes a single string argument representing a binary (base 2) integer, andreturns a string representation of that number in base 10. DecBin()Like BinDec(), but converts from base 10 to base 2. OctDec()Like BinDec(), but converts from base 8 to base 10. DecOct()Like BinDec(), but converts from base 10 to base 8. ContinuedCross- Reference31
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services