Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:31963 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27003 invoked by uid 1010); 29 Aug 2007 15:17:41 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 26988 invoked from network); 29 Aug 2007 15:17:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Aug 2007 15:17:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=zoe.slattery@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=zoe.slattery@googlemail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 64.233.182.184 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: zoe.slattery@googlemail.com X-Host-Fingerprint: 64.233.182.184 nf-out-0910.google.com Received: from [64.233.182.184] ([64.233.182.184:44763] helo=nf-out-0910.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9F/A1-17634-41E85D64 for ; Wed, 29 Aug 2007 11:17:41 -0400 Received: by nf-out-0910.google.com with SMTP id e27so201462nfd for ; Wed, 29 Aug 2007 08:17:38 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=googlemail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=R1LS6T5ESXc3xzHpsYe6XXXXkHXarCK+jKzxa4X0R/UKkeSuLzPB+1XofYjVyzgRqaliWmZuxH4VLIZ3Yab4mcyMEBVUzpK9HMjtKnYs8LHbog/qZvYeuskRygPoqoLNL41W0TT4KnLXA61FIi7lKVHWJ5Rg4YKqYuOxfYstw9M= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=BPyuSkZcWH8fWEfsLZZLNeNYu2auyRay8wEI/vuLPrtASubdOiEFQ2QbZ6/QCBPtE1q0OgNSRFvzHf6P7fFWkb3O74riJCHiHPQmIXQW3Ax6E6K3kZFJOAhjPatQoW75lfWcw4dKzv9QvSVxgqfqEvytABkNgwlY+ZGMmr4RcAU= Received: by 10.82.111.8 with SMTP id j8mr1588937buc.1188400656824; Wed, 29 Aug 2007 08:17:36 -0700 (PDT) Received: from ?9.20.187.127? ( [195.212.29.83]) by mx.google.com with ESMTPS id g30sm9321423ugd.2007.08.29.08.17.33 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 29 Aug 2007 08:17:34 -0700 (PDT) Message-ID: <46D58E08.1050401@googlemail.com> Date: Wed, 29 Aug 2007 16:17:28 +0100 User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: ceo@l-i-e.com CC: internals@lists.php.net References: <46CDA789.3080109@googlemail.com> <37683.209.254.223.2.1188099271.squirrel@www.l-i-e.com> In-Reply-To: <37683.209.254.223.2.1188099271.squirrel@www.l-i-e.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Testing math functions From: zoe.slattery@googlemail.com (Zoe Slattery) Richard Lynch wrote: > On Thu, August 23, 2007 10:28 am, Zoe Slattery wrote: > >> Hi - I've writing a few tests for the math extension and have a >> question >> about floating point precision. >> >> Here's a small example: >> >> --TEST-- >> Test return type and value for expected input sin() >> --INI-- >> precision = 14 >> --FILE-- >> > >> $threesixty = pi() * 2.0; >> echo "sin 360 = "; >> var_dump(sin($threesixty)); >> >> ?> >> --EXPECT-- >> sin 360 = float(-2.4492127076448E-16) >> >> Is it right to test for an exact number in this way? I was slightly >> suprised that I got the same number from Windows and Linux (maybe I >> shouldn't be). >> >> If not, I could write the test above to check that sin 360 is zero >> plus/minus some small number - but how small? >> > > Seems to me that if 'precision' setting is supposed to affect the > output of your calculations, then you should, in theory, be able to > rely on 14 decimal places, no?... > Actually that isn't what "precision=14" does. Precision=14 will just print out the first 14 digits of the answer ignoring leading zeros. So when I change the ini setting to precision=3, the result I get for sin(360) is -2.45E-16, this doesn't help if the processor rounding error is > 1.0 E-16 say. > I realize that's an over-simplistic answer, perhaps, and I thought the > precision only applied to BC_MATH and/or GMP calculations. > I'm sorry - I don't know enough about BC_MATH or GMP to comment. > I'd be surprised if it was ALWAYS right for every OS, and I strongly > suspect it's going to fail on 64-bit hardware big-time. > As Piere says - and I just verified by using a few different processors - the answer is processor dependent. > I guess the first question I have is "What precisely are you testing?" > > The 'precision' setting? > Nope :-) > Or just that sin(2 * M_PI) is kinda sorta close to 0? > Yes - the most simple test of sin() or cos() functions is that they should give the expected result for known values. > If you just want "close to 0" then double the answer you are getting > now, and call that "close enough" :-) > Well - maybe. After trying a few different processors a reasonable thing to do seems to be to check that the answer of a floating point calculation is within "plus or minus 1.0E-10" of the expected result. This feels a bit arbitrary - but there doesn't seems to be a better solution (except maybe a lot of research into allowable rounding errors from each sort of processor technology and I don't think this particular case warrants that) > If you think 'precision' is supposed to affect it and guarantee 14 > decimal places of precision, then 1.0E-14 to 1.0E14 should be your > range, no? > > See above