Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

probleme d'arrondi #2972

Closed
vicuna opened this issue Sep 26, 2001 · 2 comments
Closed

probleme d'arrondi #2972

vicuna opened this issue Sep 26, 2001 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Sep 26, 2001

Original bug ID: 545
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

Bonjour

(I hope you speak french... Tell me otherwise, I can translate... :-))

j'ai ete confronte au probleme suivant (peut etre classique, mais

je ne l'ai vu dans aucun bouquin) :

% ocaml
Objective Caml version 3.02

let rec h n a b = if (n=0) then b else h (n-1) a (b+.a);;

val h : int -> float -> float -> float =

h 100 0.01 0.;;

  • : float = 1

1. <= h 100 0.01 0.;;

  • : bool = true

h 10000 0.0001 0.;;

  • : float = 1

1. <= h 10000 0.0001 0.;;

  • : bool = false

Il y a manifestement un probleme d'arrondi, mais neanmoins la valeur
affichee est correcte.

Le meme bug se produit avec ocaml 2.04...

Amicalement,

--
Nicolas MARKEY

@vicuna
Copy link
Author

vicuna commented Sep 26, 2001

Comment author: administrator

On Wed, Sep 26, 2001 at 08:14:47PM +0200, markey@lsv.ens-cachan.fr wrote:

let rec h n a b = if (n=0) then b else h (n-1) a (b+.a);;

val h : int -> float -> float -> float =

h 100 0.01 0.;;

  • : float = 1

1. <= h 100 0.01 0.;;

  • : bool = true

h 10000 0.0001 0.;;

  • : float = 1

1. <= h 10000 0.0001 0.;;

  • : bool = false

Que ce soit en Fortran, en C, en Pascal ou en OCaml, les opérations
sur les flottants ne sont jamais garanties d'être exactes. L'affichage
"1" ne veut pas dire que c'est exactement "1". Si ça se trouve, c'est
0.9999999832 ou 1.00000547. Les flottants ne sont pas des nombres réels
mais des approximations.

En particulier, l'égalité ne devrait jamais être utilisée: au lieu du
test x = y, ont doit écrire abs (x - y) < eps (eps étant une valeur
convenablement choisie). Les opérations "<=" et ">=" sur les flottants
ne devraient pas être utilisées.

C'est du moins ce qu'on m'a appris à l'école, à une époque où Caml
n'existait pas mais c'est toujours les mêmes flottants.

D'ailleurs - petite remarque à mes chers collègues - je ne comprends
pas bien pourquoi il existe un pattern matching sur les flottants qui
va à l'encontre de cette idée...

--
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/

@vicuna
Copy link
Author

vicuna commented Sep 27, 2001

Comment author: administrator

let rec h n a b = if (n=0) then b else h (n-1) a (b+.a);;

val h : int -> float -> float -> float =

h 10000 0.0001 0.;;

  • : float = 1

1. <= h 10000 0.0001 0.;;

  • : bool = false

Il y a manifestement un probleme d'arrondi, mais neanmoins la valeur
affichee est correcte.

Le toplevel Caml affiche les flottants avec environ 12 chiffres
significatifs, mais calcule avec une plus grande précision. Un nombre
peut s'afficher comme "1" et ne pas être égal à "1". Démonstration:

let rec h n a b = if (n=0) then b else h (n-1) a (b+.a);;

val h : int -> float -> float -> float =

let x = h 10000 0.0001 0.;;

val x : float = 1

x -. 1.0;;

  • : float = -9.38138455808e-14

Printf.printf "%.20g\n" x;;

0.99999999999990618615

  • : unit = ()

L'arithmétique flottante est ainsi faite: pleine de surprises :-)

  • Xavier Leroy

@vicuna vicuna closed this as completed Oct 3, 2001
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant