If you ever use a programming language to calculate something you will use the floor, the ceil, the round functions. It's freaky, but the result of these function differs. Here it is the source codes, and the output results. Funny ActionsScript and obviously Java :)
PHP version:
<?php
$testit = array( 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 );
echo "<table>";
foreach( $testit as $test ) {
echo "<tr>";
echo "<td>floor( {$test} )=" . floor( $test ) . "</td>";
echo "<td>round( {$test} )=" . round( $test ) . "</td>";
echo "<td>ceil( {$test} )=" . ceil( $test ) . "</td>";
echo "</tr>";
}
?>
PHP result:
floor( 1.7 )=1 round( 1.7 )=2 ceil( 1.7 )=2
floor( 1.5 )=1 round( 1.5 )=2 ceil( 1.5 )=2
floor( 0.3 )=0 round( 0.3 )=0 ceil( 0.3 )=1
floor( -0.3 )=-1 round( -0.3 )=-0 ceil( -0.3 )=-0
floor( -1.5 )=-2 round( -1.5 )=-2 ceil( -1.5 )=-1
floor( -1.7 )=-2 round( -1.7 )=-2 ceil( -1.7 )=-1
Freepascal version:
program MathDemo;
uses Math;
const
testit : array[1..6] of double = ( 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 );
var
i : integer;
begin
for i := 1 to length( testit ) do begin
write( 'floor(', testit[i],')=', floor( testit[i] ),' ' );
write( 'round(', testit[i],')=', round( testit[i] ),' ' );
writeln( 'ceil(' , testit[i],')=', ceil( testit[i] ),' ' );
end;
end.
Freepascal result:
floor( 1.70000000000000E+000)=1 round( 1.70000000000000E+000)=2 ceil( 1.70000000000000E+000)=2
floor( 1.50000000000000E+000)=1 round( 1.50000000000000E+000)=2 ceil( 1.50000000000000E+000)=2
floor( 3.00000000000000E-001)=0 round( 3.00000000000000E-001)=0 ceil( 3.00000000000000E-001)=1
floor(-3.00000000000000E-001)=-1 round(-3.00000000000000E-001)=0 ceil(-3.00000000000000E-001)=0
floor(-1.50000000000000E+000)=-2 round(-1.50000000000000E+000)=-2 ceil(-1.50000000000000E+000)=-1
floor(-1.70000000000000E+000)=-2 round(-1.70000000000000E+000)=-2 ceil(-1.70000000000000E+000)=-1
GCC version:
#include <stdio.h>
#include <stdlib.h>
int main() {
double testit[] = { 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 };
int i;
for ( i=0 ; i< sizeof( testit ) / sizeof( testit[0] ); i++ ) {
printf("floor(%f)=%f round(%f)=%f, ceil(%f)=%f\n", testit[i], floor( testit[i] ), testit[i], round( testit[i] ), testit[i], ceil( testit[i] ) );
}
return 0;
}
GCC result:
floor(1.700000)=1.000000 round(1.700000)=2.000000, ceil(1.700000)=2.000000
floor(1.500000)=1.000000 round(1.500000)=2.000000, ceil(1.500000)=2.000000
floor(0.300000)=0.000000 round(0.300000)=0.000000, ceil(0.300000)=1.000000
floor(-0.300000)=-1.000000 round(-0.300000)=-0.000000, ceil(-0.300000)=-0.000000
floor(-1.500000)=-2.000000 round(-1.500000)=-2.000000, ceil(-1.500000)=-1.000000
floor(-1.700000)=-2.000000 round(-1.700000)=-2.000000, ceil(-1.700000)=-1.000000
ActionScript 3 version:
public var testit : Array = [ 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 ];
public function inittest() : void {
var res : String = "";
for each( var test : Number in testit ) {
res += "Math.floor(" + test + ")=" + Math.floor( test ) + " ";
res += "Math.round(" + test + ")=" + Math.round( test ) + " ";
res += "Math.ceil(" + test + ")=" + Math.ceil( test ) + "\n";
}
result.text = res;
}
ActionScript 3 result:
Math.floor(1.7)=1 Math.round(1.7)=2 Math.ceil(1.7)=2
Math.floor(1.5)=1 Math.round(1.5)=2 Math.ceil(1.5)=2
Math.floor(0.3)=0 Math.round(0.3)=0 Math.ceil(0.3)=1
Math.floor(-0.3)=-1 Math.round(-0.3)=0 Math.ceil(-0.3)=0
Math.floor(-1.5)=-2 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1
Math.floor(-1.7)=-2 Math.round(-1.7)=-2 Math.ceil(-1.7)=-1
Java version:
public class jmath
{
public static void main (String[] args)
{
double[] testit = { 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 };
String res = "";
for ( int i=0 ; i < testit.length; i++ ) {
res = "Math.floor(" + testit[i] + ")=" + Math.floor( testit[i] ) + " ";
res += "Math.round(" + testit[i] + ")=" + Math.round( testit[i] ) + " ";
res += "Math.ceil(" + testit[i] + ")=" + Math.ceil( testit[i] );
System.out.println( res );
}
}
}
Java result:
Math.floor(1.7)=1.0 Math.round(1.7)=2 Math.ceil(1.7)=2.0
Math.floor(1.5)=1.0 Math.round(1.5)=2 Math.ceil(1.5)=2.0
Math.floor(0.3)=0.0 Math.round(0.3)=0 Math.ceil(0.3)=1.0
Math.floor(-0.3)=-1.0 Math.round(-0.3)=0 Math.ceil(-0.3)=-0.0
Math.floor(-1.5)=-2.0 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1.0
Math.floor(-1.7)=-2.0 Math.round(-1.7)=-2 Math.ceil(-1.7)=-1.0
C# version:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sharp2008
{
class Program
{
static void Main(string[] args)
{
double[] testit = { 1.7, 1.5, 0.3, -0.3, -1.5, -1.7 };
String res = "";
for ( int i=0 ; i < testit.Length; i++ ) {
res = "Math.floor(" + testit[i] + ")=" + Math.Floor( testit[i] ) + " ";
res += "Math.round(" + testit[i] + ")=" + Math.Round( testit[i] ) + " ";
res += "Math.ceil(" + testit[i] + ")=" + Math.Ceiling( testit[i] );
System.Console.WriteLine( res );
}
}
}
}
C# result:
Math.floor(1,7)=1 Math.round(1,7)=2 Math.ceil(1,7)=2
Math.floor(1,5)=1 Math.round(1,5)=2 Math.ceil(1,5)=2
Math.floor(0,3)=0 Math.round(0,3)=0 Math.ceil(0,3)=1
Math.floor(-0,3)=-1 Math.round(-0,3)=0 Math.ceil(-0,3)=0
Math.floor(-1,5)=-2 Math.round(-1,5)=-2 Math.ceil(-1,5)=-1
Math.floor(-1,7)=-2 Math.round(-1,7)=-2 Math.ceil(-1,7)=-1
No comments:
Post a Comment