Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > fr.comp.lang.python > #3947

Re: Affichage en notation scientifique.

From Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid>
Newsgroups fr.comp.lang.python
Subject Re: Affichage en notation scientifique.
Date 2022-09-14 16:37 +0200
Organization Université de Strasbourg
Message-ID <87a672xbrj.fsf@universite-de-strasbourg.fr.invalid> (permalink)
References <tfsmg1$2vqdl$1@dont-email.me>

Show all headers | View raw


Dominique <zzz@aol.com> writes:

> Je m'amuse avec « 100 énigmes mathématique123654789s résolues avec Python »
>
> Soit t=1236354, est-il possible d'avoir ce nombre en notation
> scientifique 1.236354e6 et récupérer dans une variable l'exposant (6
> ici) ?

Il y a deux choses complètement différentes dans ta question :

1) pour formater un flottant en notation scientifique :

    "%e" % (123456.0) # ou "{:e}".format (123456.0)

   avec des tas de variantes (cf.
   https://docs.python.org/3/library/string.html#formatspec)

2) Pour extraire mantisse est exposant, il y a math.frexp, mais

    >>> math.frexp (123456.0)
    (0.94189453125, 17)

   Tout est normal : dans le codage IEEE 754 (utilisé par Python et le
   reste du monde), mantisse et exposant sont codés en binaire (pour le
   dire vite), et c'est ça qu'on récupère. Bien sûr, m*2**e est égal au
   nombre initial.

   Si par contre tu veux tout cela "en base 10", tu peux jongler avec
   math.floor (math.log10 (...)), qui te donnera l'exposant. Il faut
   ensuite diviser explicitement (et gérer le signe à part). Je ne
   connais pas de raccourci.

-- Alain.

Back to fr.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Affichage en notation scientifique. Dominique <zzz@aol.com> - 2022-09-14 15:55 +0200
  Re: Affichage en notation scientifique. Dominique <zzz@aol.com> - 2022-09-14 16:17 +0200
  Re: Affichage en notation scientifique. Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-09-14 16:37 +0200
    Re: Affichage en notation scientifique. Dominique <zzz@aol.com> - 2022-09-14 17:25 +0200
      Re: Affichage en notation scientifique. Dominique <zzz@aol.com> - 2022-09-14 18:33 +0200
      Re: Affichage en notation scientifique. Alain Ketterlin <alain@universite-de-strasbourg.fr.invalid> - 2022-09-14 18:59 +0200

csiph-web