[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: calculation error with built-in command CTRL + J?!
From: |
Sven Joachim |
Subject: |
Re: calculation error with built-in command CTRL + J?! |
Date: |
Mon, 14 Apr 2008 08:40:13 +0200 |
User-agent: |
Gnus/5.110008 (No Gnus v0.8) Emacs/22.2.50 (gnu/linux) |
On 2008-04-14 05:59 +0200, Andreas Borde wrote:
> I calculated
> (* 12 2.6) with the built in command CTRL + J and got
> 31.200000000000003
A normal rounding error for floating point numbers.
> It is the same for
> (* 2.6 12)
Multiplication is commutative. ;-)
> I am wondering about the tailing numbers after 31.2!
The computer can only represent real numbers that are a linear
combination of (possibly negative) powers of 2, all other numbers can
only be approximated. See http://en.wikipedia.org/wiki/Floating_point
for details.
> If I calculate (* 2.5 12) then the result is the expected 30.0.
Because 2.5 = 2^1 + 2^(-1), therefore the computer can calculate exactly.
> Can someone tell me the reason? Is there something wrong with
> my calcuation?
No, there's nothing wrong. The following little C program prints the
same results as Emacs:
--8<---------------cut here---------------start------------->8---
#include <stdio.h>
int main(void)
{
printf("12 * 2.6 = %.15f\n", 12 * 2.6);
return 0;
}
--8<---------------cut here---------------end--------------->8---
Regards,
Sven