PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] Why is the scientific formatting of Decimal(0) different from float 0?

 August 10, 2022     decimal, formatting, python, python-3.x     No comments   

Issue

When using Decimal(0) and formatting it to .2e format the following happens:

>>> f'{Decimal(0):.2E}'
'0.00E+2'

However if you just use 0 or 0. the following happens:

>>> f'{0.:.2E}'
'0.00E+00'

How come the results are different?


Solution

In my previous answer I described how this is done in cpython, in this I will describe why. This question was raised in this discussion and most of the quotes will be from there:

Let's consider an example:

>>> x = Decimal("1e+5").quantize(Decimal("1e+10"))
>>> x
Decimal('0E+10')
>>> s = "{:.19e}".format(x)
>>> s
'0.0000000000000000000e+29'
>>> Decimal(s)
Decimal('0E+10')

The original magnitude was e+10, after formatting it's still e+10. The magnitude of the original number is kept after the formatting process.

From the point of view of decimal it's the right thing. The original magnitude should be traceable

0 is really special in the IBM specification. The magnitude is kept, the precision is not.

>>> Decimal("0e10") * Decimal("0e20") 
Decimal('0E+30')


>>> Decimal("0.000e10")
Decimal('0E+7')

So we're basically doing the reverse of the above in formatting when a precision is given.


So, if we go back to the original example of the OP:

>>> f'{Decimal(0):.2E}'
'0.00E+2'

If it returned 0.00Е+00 then the magnitude of this decimal number would be E-2:

>>> d = f'{Decimal(0):.2E}'
>>> d
'0.00E+2'
>>> Decimal(d)
Decimal('0')
>>> d = '0.00E+00'
>>> Decimal(d)
Decimal('0.00')


Answered By - alex_noname
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing