Zulip Chat Archive

Stream: maths

Topic: Is there a Float.pi


Colin Jones ⚛️ (Mar 24 2025 at 02:38):

Is there in Mathlib a Float.Pi like Real.Pi that is defined as a floating point number and is computable?

Walter Moreira (Mar 24 2025 at 23:53):

The file Init/Data/Float.lean in Lean's source code contains the basic definitions for Float. There doesn't seem to have constants for Pi or e.

You could potentially define your own with:

def fPi : Float := Float.atan2 0 (-1)
/--
info: 3.141593
-/
#guard_msgs in
#eval fPi

Colin Jones ⚛️ (Mar 25 2025 at 00:39):

I'm looking at the documentation and Float.atan2 and other functions are defined as "opaque". What does opaque mean?

Eric Wieser (Mar 25 2025 at 01:07):

That you can't use its definition in a proof

Colin Jones ⚛️ (Apr 01 2025 at 01:24):

Is it possible to do something like this:

def Pi {α : Type} :=
  if α =  then
    π
  else
    Float.atan2 0 (-1)

Aaron Liu (Apr 01 2025 at 01:46):

Colin Jones ⚛️ said:

Is it possible to do something like this:

def Pi {α : Type} :=
  if α =  then
    π
  else
    Float.atan2 0 (-1)

Yes, but it will have to be noncomputable

Colin Jones ⚛️ (Apr 01 2025 at 01:53):

So pointless if I wanted to use it as a Float?

Aaron Liu (Apr 01 2025 at 01:53):

You can do something similar with typeclasses that does compute.

Aaron Liu (Apr 01 2025 at 01:58):

for example purposes

import Mathlib

class HasPi (α : Type*) where
  pi : α

notation "π" => HasPi.pi

noncomputable instance : HasPi  where
  pi := Real.pi

instance : HasPi Float where
  pi := Float.atan2 0 (-1)

#eval (π : Float)

theorem Real.pi_eq_pi : π = Real.pi := rfl

Colin Jones ⚛️ (Apr 01 2025 at 03:03):

Thank you!


Last updated: May 02 2025 at 03:31 UTC