librelist archives

« back to archive

Very strange artihmetic

Very strange artihmetic

From:
Date:
2011-10-09 @ 08:34
Hi all,

The class below implements some tests :

class
	ESSAIS_REAL_32

insert
	PLATFORM

create
	make

feature

	Cent : REAL_32 is 100.0
	
feature

	make is
		local
			x : REAL_32
		do
			print("Cent = " + Cent.out + "%N%N")
			
			x := Minimum_real_32 + Cent
			print("      m = " + Minimum_real_32.out + "%N")
			print("m + 100 = " +  x.out + "%N%N")

			if
				x ~= Minimum_real_32
			then
				print("(m + 100) ~= m : Oh! Oh!%N")
			end
			
			print("(m + 100) - m = " + (x - Minimum_real_32).to_string_format(128) + "%N")
		end

	
end -- class ESSAIS_REAL_32

The results are :

Cent = 100.000000

      m = -340282346638528859811704183484516925440.000000
m + 100 = -340282346638528859811704183484516925440.000000

(m + 100) ~= m : Oh! Oh!
(m + 100) - m = 0.0000000000000 etc 0000

What do you think about these tests ?

Regards.

Christophe

Re: [libertyeiffel] Very strange artihmetic

From:
Cyril Adrian
Date:
2011-10-09 @ 08:56
2011/10/9 <christophe.haro@free.fr>
>
> What do you think about these tests ?
>

Nothing unbelievable. 100 is so far below the radar of the large
floating-point number as not to blip a single bit.

Remember that a floating point is above all a mantissa, and fractional part
that represents the most significant part of the number.

The order of magnitude I found that started making any difference for your
operation is 100000000000000000000000000000000.0 (internally,
100000003318135351409612647563264.000000) :-)

As for ~= the magnitude was one zero more!

Floating point should not be used if you need accuracy. That's the rule
number one in banks :-)

Cheers,


 *Cyril ADRIAN
**http://www.cadrian.net/cyril*
Share with me: [image: Facebook]
<https://www.facebook.com/cyril.adrian> [image:
Twitter] <http://www.twitter.com/cadbart> [image:
LinkedIn]<http://www.linedin.com/cyril.adrian> [image:
Google Plus] <http://gplus.to/cadrian>
Contact me: [image: Google Talk] cyril.adrian@gmail.com
 [image: Twitter] <http://twitter.com/cadbart> Latest tweet: [Liberty]
http://t.co/LxkBu44e Cyril Adrian - fix #16: remove dependency from cpp to
runner
Follow @cadbart <http://twitter.com/cadbart> Reply

<http://twitter.com/?status=@cadbart%20&in_reply_to_status_id=122304618154827780&in_reply_to=cadbart>
Retweet

<http://twitter.com/?status=RT%20%40cadbart%3A%20%5BLiberty%5D%20http%3A%2F%2Ft.co%2FLxkBu44e%20Cyril%20Adrian%20-%20fix%20%2316%3A%20remove%20dependency%20from%20cpp%20to%20runner>
 15:37 Oct-07<http://twitter.com/cadbart/statuses/122304618154827776>
  Get this email app!

<http://www.wisestamp.com/apps/twitter?utm_source=extension&utm_medium=email&utm_term=twitter&utm_campaign=apps>

[image: My QR VCard]
  Get a signature like this.

<http://r1.wisestamp.com/r/landing?promo=19&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_19>
CLICK

HERE.<http://r1.wisestamp.com/r/landing?promo=19&dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_19>