Skip to content

Instantly share code, notes, and snippets.

@edwardgeorge
Created February 13, 2018 09:28
Show Gist options
  • Save edwardgeorge/82f882fa2338b7b5dc9dd84389a0409d to your computer and use it in GitHub Desktop.
Save edwardgeorge/82f882fa2338b7b5dc9dd84389a0409d to your computer and use it in GitHub Desktop.
from collections import namedtuple
from functools import reduce
Scientific = namedtuple("Scientific", "coefficient exponent")
def decimal_to_scientific(dec):
sign, digits, exp = dec.as_tuple()
coeff = reduce(lambda x, y: (x * 10) + y, digits, 0)
return Scientific(-coeff if sign else coeff, exp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment