Scratching my head at what seems like the simplest thing ever.
I am working on a polynomial calculator and have been testing it with some values, but python is not adding these two values the way it needs to be.
ans_2 = (-0.423604188255 + 0.42368554704)
print("Ans 2 = : " + str(ans_2))
I am getting this answer:
Ans 2 = : 8.1358785e-05
But I should be getting an answer like this:
=0.00009
Is there something I am doing wrong?
The value 8.1358785e-05 is scientific notation for the value .000081358785. The print function automatically converts to scientific notation for floats under a certain value.
If you want it to print out the value you're looking for, you need to format the string to tell the print function exactly what you're looking for. You can do that like this
In [11]: ans_2 = (-0.423604188255 + 0.42368554704)
In [12]: print("Ans 2 = : {:.7f} ".format(ans_2))
Ans 2 = : 0.0000814
This will format the string to include seven digits after the decimal.
The value 8.1358785e-05 is just another representation of 0.000081358785.
Whenever you see 'e' in your number means that your value was multiplied by 10 to power of anything that goes after 'e'.
In your case it will be:
8.1358785 * 10 ^ (-5) = 0.000081358785
For more info look there
Possibly you are overlooking that there is -ve sign before the first number. That's why you seem to expect the function to return 0.847289735295 (sum of absolute values of operands) instead of the correct answer (their difference according to their sign)
Related
I already tried converting it to a string, adding 0 to it, but it still only shows me the decimal value, how can I make it show me its binary value?
If I try this:
binary = 0b010
print(binary)
or this:
binary = 0b010
print(str(binary))
which outputs this:
2
I also tried this:
binary = 0b010
print("{0:b}".format(binary))
which outputs this:
10
Desired output:
010
The bad part is that it does not fill in the number of 0 on the left, it gives me an output of 10 when I want 010, but I want it to show the number of 0 possible for any binary that I put.
I'm new to Python, but every time I look for how to do it, it only appears to me as converting from integer to binary and showing it. But I can't use the bin() function, so I resorted to asking it here directly, thanks for your understanding.
You have a couple of ways of going about it.
The one that fits your use case the best is the format() method:
binary = 0b010
print("{:03b}".format(binary))
Which outputs:
010
Changing the 3 in {:03b} will change the minimum length of the output(it will add leading zeros if needed).
If you want to use it without leading zeros you can simply do {:b}.
You can try defining your own function to convert integers to binary:
def my_bin(x):
bit = ''
while x:
bit += str(x % 2)
x >>= 1
return '0' + bit[::-1]
binary = 0b010
print(my_bin(binary))
Output:
010
i have some doubts in using round function correctly ,i want to know how i should use the round function correctly without any problem.
this is how how i coded in python code:
form1:
res = round(float(float(self.inst) - int(self.elt)),1)
also if put:
form2:
res1 = round(2*float(self.pro),1)
--->it doesn't give me any result .
in form1 ,when i put integer values it gives me result but when i put float values , nothing shown .help please i want those lignes of code work normal , with float and integer values.
in form2 nothing shown even i put integer or float values to see if the sentence work.
From round() documentation :
Return number rounded to ndigits precision after the decimal point. If
ndigits is omitted or is None, it returns the nearest integer to its
input.
Rounding an integer will always return the integer you passed to it, no matter what :
round(42, 50)
42
The second parameter is used to ask for a certain number of digit after the dot for floats.
round(1.2)
1
Cause 0 digit after dot is asked.
round(1.2, 1)
1.2
Cause 1 digit after dot is asked.
round(1.2,2)
1.2
2 digits are asked, but the floating number gave in parameter has only one digit.
In your case res1 and res are highly dependent of self.inst self.elt and self.pro so we cannot help you if you are not more specific. I suggest you write your code with one line by operation and check the results at each steps. I think your problem is not coming from the round() function, but the operations inside it.
By the way nobody will figure out what you try to achieve with those variable names.
You dont need the extra float conversion.
I am getting the below result -
a = '14.524'
b = 7.890
res = round((float(a) - int(b)), 1)
print(res)
7.5
res1 = round(2 * float(a), 1)
print(res1)
29
Check for errors in your terminal window.
I want to know if there is any way to differentiate a whole number from any other output only using maths, eg if you have the number 5 I would like to convert that into the number 0 using equations, however, the number 5.4342 would output the number -1
(What i am trying to do is very hard to put into words so i can clear up any questions)
Use type() to check if it's an integer or a float. In python, it's usually best to do it this way.
Mathematically, if you allow integer divisions, you could use this:
def isInt(n): return 0**(n-n//1)-1
isInt(5) # 0
isInt(5.4342) # -1.0
If can also work with a modulo:
def isInt(n): return 0**(n%1)-1
I have a ridiculous code segment in one of my programs right now:
str(len(str(len(var_text)**255)))
Is there an easy way to shorten that? 'Cause, frankly, that's ridiculous.
A option to convert a number >500 digits to scientific notation would also be helpful
(that's what I'm trying to do)
Full code:
print("Useless code rating:" , str(len(var_text)**255)[1] + "e" + str(len(str(len(var_text)**255))))
TL;DR: y = 2.408 * len(var_text)
Lets assume that your passkey is a string of characters with 256 characters available (0-255). Then just as a 16bit number holds 65536 numbers (2**16) the permutations of a string of equal length would be
n_perms = 256**len(passkey)
If you want the number of (decimal) digits in n_perms, consider the logarithm:
>>> from math import log10
>>> log10(1000)
3.0
>>> log10(9999)
3.9999565683801923
>>>
So we have length = floor(log10(n_perms)) + 1. In python, int rounds down anyway, so I'd say you want
n_perms = 256**len(var_text)
length = int(log10(n_perms)) + 1
I'd argue that 'shortening' ugly code isn't always the best way - you want it to be clear what you're doing.
Edit: On further consideration I realised that choosing base-10 to find the length of your permutations is really arbitrary anyway - so why not choose base-256!
length = log256(256**len(var_text)
length = len(var_text) # the log and exp cancel!
You are effectively just finding the length of your passkey in a different base...
Edit 2: Stand back, I'm going to attempt Mathematics!
if x = len(var_text), we want y such that
y = log10(256**x)
10**y = 256**x
10**y = (10**log10(256))**x
10**y = (10**(log10(256)x))
y = log10(256) * x
So, how's this for short:
length = log10(256) * len(var_text) # or about (2.408 * x)
This looks like it's producing a string version of the number of digits in the 255th power of the length of a string. Is that right? I'd be curious what that's used for.
You could compute the number differently, but it's not shorter and I'm not sure it's prettier:
str(int(math.ceil(math.log10(len(var_text))*255)))
or:
"%d" % math.ceil(math.log10(len(v))*255)
Are you trying to determine the number of possible strings having the same length as var_text? If so, you have your base and exponent reversed. You want to use 255**len(var_text) instead of len(var_text)**255.
But, I have to ask ... how long do these passkeys get to be, and what are you using them for?
And, why not just use the length of the passkey as an indicator of its length?
Firstly, if your main problem is manipulating huge floating point expressions, use the bigfloat package:
>>> import bigfloat
>>> bigfloat.BigFloat('1e1000')
BigFloat.exact('1.0000000000000001e+1000', precision=53)
As for the details in your question: len(str(num)) is approximately equal to log(num, 10) + 1. This is not significantly shorter, but it's certainly a better way of expressing it in code (for the benefit of anyone who doesn't know that off the top of their head). You can then simplify it with log laws:
len(str(x**y))
= log(x**y, 10) + 1
= y * log(x, 10) + 1
So maybe you'll find:
"%i" % (log(len(var_text),10)*255 + 1)
... is better? It's not significantly shorter, but it's a much clearer mathematical relationship between input and output.
111111111111111111111111111111111111111111111111111111111111
when i take this as input , it appends an L at the end like this
111111111111111111111111111111111111111111111111111111111111L
thus affecting my calculations on it .. how can i remove it?
import math
t=raw_input()
l1=[]
a=0
while (str(t)!="" and int(t)!= 0):
l=1
k=int(t)
while(k!= 1):
l=l+1
a=(0.5 + 2.5*(k %2))*k + k % 2
k=a
l1.append(l)
t=raw_input()
a=a+1
for i in range(0,int(a)):
print l1[i]
this is my code and it works for every test case except 111111111111111111111111111111111111111111111111111111111111
so i guess something is wrong when python considers such a huge number
It looks like there are two distinct things happening here. First, as the other posters have noted, the L suffix simply indicates that Python has converted the input value to a long integer. The second issue is on this line:
a=(0.5 + 2.5*(k %2))*k + k % 2
This implicitly results in a floating point number for the value of (0.5 + 2.5*(k %2))*k. Since floats only have 53 bits of precision the result is incorrect due to rounding. Try refactoring the line to avoid floating point math, like this:
a=(1 + 5*(k %2))*k//2 + k % 2
It's being input as a Long Integer, which should behave just like any other number in terms of doing calculations. It's only when you display it using repr (or something that invokes repr, like printing a list) that it gets the 'L'.
What exactly is going wrong?
Edit: Thanks for the code. As far as I can see, giving it a long or short number makes no difference, but it's not really clear what it's supposed to do.
As RichieHindle noticed in his answer, it is being represented as a Long Integer. You can read about the different ways that numbers can be represented in Python at the following page.
When I use numbers that large in Python, I see the L at the end of the number as well. It shouldn't affect any of the computations done on the number. For example:
>>> a = 111111111111111111111111111111111111111
>>> a + 1
111111111111111111111111111111111111112L
>>> str(a)
'111111111111111111111111111111111111111'
>>> int(a)
111111111111111111111111111111111111111L
I did that on the python command line. When you output the number, it will have the internal representation for the number, but it shouldn't affect any of your computations. The link I reference above specifies that long integers have unlimited precision. So cool!
Another way to avoid numerical errors in python is to use Decimal type instead of standard float.
Please refer to official docs
Are you sure that L is really part of it? When you print such large numbers, Python will append an L to indicate it's a long integer object.