Python‎ > ‎decimal‎ > ‎

Decimal.shift()


Definition
Return the result of shifting the digits of the first operand by an amount specified by the second operand.

Syntax
Decimal(x).shift(other, context=None) 

Parameters
x=Decimal
y=integer

Examples
The return are as follows:
 >>> 
>>> Decimal(1).shift(1)
Decimal('10')
>>> Decimal(1).shift(10)
Decimal('10000000000')
>>> Decimal(0).shift(1)
Decimal('0')
>>> Decimal(1).shift(0)
Decimal('1')
>>> Decimal(-1).shift(-1)
Decimal('-0')
>>> Decimal(10).shift(2)
Decimal('1000')
>>> 

The method return with the difference in the set items:
from decimal import Decimal

set1={Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),Decimal(1.5000001).to_integral(), Decimal(10).shift(2)}
set2={ Decimal(1).scaleb(0), Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}

print(set1.copy())

>>>
{Decimal('1000'), Decimal('0'), Decimal('2')}
>>>



The returns are following:
from decimal import Decimal
import itertools

set1={Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),
                        Decimal(1.5000001).to_integral()Decimal(10).shift(2)}
set2={Decimal(1).scaleb(0),Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}

print("The decimal values of set1 are:", set1)
print("The decimal values of set2 are: ", set2)

max_set1=int(max({Decimal(0.1).to_integral_value(),Decimal(1.9).to_integral_exact(),
Decimal(1.5000001).to_integral(), Decimal(10).shift(2)}))
print("The maximum integer value in set1 is: ",max_set1)

min_set2=int(min({Decimal(1).scaleb(0),Decimal(1).remainder_near(2),Decimal(1.0000).normalize()}))
print("The minimum integer value in set2 is:",min_set2)

set3=max_set1.__add__(min_set2)
print("The set1 added by set2 is: ", set3) 

set4=max_set1.__lt__(min_set2)
print("The max_set1 is lessor than min_set2:", set4)

x=set1.difference(set2)
rint("The set1 difference to set2 is :", x)

>>> 
The decimal values of set1 are: {Decimal('1000'), Decimal('0'), Decimal('2')}
The decimal values of set2 are: {Decimal('1')}
The maximum integer value in set1 is: 1000
The minimum integer value in set2 is: 1
The set1 added by set2 is: 1001
The max_set1 is lessor than min_set2: False
The set1 difference to set2 is : {Decimal('1000'), Decimal('0'), Decimal('2')}
>>>




Comments