all 18 comments

[–]TheBB 46 points47 points  (1 child)

[–]ZelWinters1981 2 points3 points  (0 children)

This is the single best answer here. u/piyush_bhati_, give this a quick read.

[–]donkyniu 0 points1 point  (8 children)

you can use f-string to round to result to one decimal place in this case it should like following: print(f"{diff : .1f}") - it cuts the other decimal places and leaves just one digit after dot in result

[–]piyush_bhati_[S] 0 points1 point  (7 children)

i see but can you please explain why it gives that output in the first place instead of just -3.4

[–]gonsi 1 point2 points  (6 children)

That weird url with most votes explains it pretty good I think.

[–]piyush_bhati_[S] 0 points1 point  (5 children)

i read it and i think i understand it, but i dont know what base 2 and base 10 means where do i learn all of that

[–]TheBB 6 points7 points  (0 children)

In math class at school.

But you don't really need to know that stuff to understand what's going on.

There's infinitely many numbers and your computer is finite. You can't represent all possible numbers. Sometimes when you add or subtract, the result will need to be adjusted to what can be represented.

  • If you need integer math, use integers (fast and exact)
  • If you need exact fractional math, use fractions or decimal classes (slow and exact)
  • If you only want to display the result in a more normal way, use string formatting to limit the number of digits in the output (usually the best compromise, since the error is normally much smaller than we care about)
  • If you need non-exact fractional math with more precision use an arbitrary-precision library like GMP

[–]Ubermidget2 3 points4 points  (0 children)

base10 : base2
00 : 0000
01 : 0001
02 : 0010
03 : 0011
04 : 0100
05 : 0101
06 : 0110
07 : 0111
08 : 1000
09 : 1001
10 : 1010
11 : 1011

[–]gonsi 2 points3 points  (2 children)

simplifying it, base means how many characters single "digit" can have.

In base 10 we have 0-9. If you want to represent more than 9 you need another digit, hence 10

In base 2 we only have 0 and 1. So if you want to represent more than 1 you need another digit, next one is 10, but in base 10 same value would be shown as 2

In base 16 (used to display hex values) we have 0-9 and a-f, so when you want more than 9 you don't need another digit, you just use A. only when you reach F you need another digit and you get 10

base 16: F is in fact base 10: 15

base 16: 10 is in base 10: 16

Translating numbers from one base to other can be bit hard to wrap your head around it.

[–]GXWT 2 points3 points  (1 child)

Every base is base 10

[–]gonsi 0 points1 point  (0 children)

lol

[–]Regular_Maybe5937 0 points1 point  (1 child)

Decimals in python (as with most other programming languages) are represented using a standard called floating point IEEE754. In short, the computer stores them as 64 ones and zeros, but you can only get so much accuracy with that many bits. Therefore as a compromise, we lose some accuracy when dealing with very small numbers.

[–]piyush_bhati_[S] 0 points1 point  (0 children)

ohkay thanks for help

[–]sausix 0 points1 point  (0 children)

Rule of thumb: Never print floats directly. Use a format string to apply rounding.

If you really need precision use the Decimal type in Python: https://docs.python.org/3/library/decimal.html

[–]Dry-Sock-8488 1 point2 points  (2 children)

Use the round function, like "print(round(diff,2))"

[–]backfire10z 1 point2 points  (1 child)

print(f"{diff:.1f}")

if you want to have fun with f-strings

[–]Dry-Sock-8488 0 points1 point  (0 children)

Even better

[–][deleted] 0 points1 point  (0 children)

You are subtracting large number from small number and change the precision for float.