floating-point

Avoid overflow in random matrix multiplication in python

Avoid overflow in random matrix multiplication in python Question: I have to multiply many (about 700) matrices with a random element (in the following, I’m using a box distribution) in python: #define parameters μ=2. σ=2. L=700 #define random matrix T=[None]*L product=np.array([[1,0],[0,1]]) for i in range(L): m=np.random.uniform(μ-σ*3**(1/2), μ+σ*3**(1/2)) #box distribution T[i]=np.array([[-1,-m/2],[1,0]]) product=product.dot(T[i]) #multiplying matrices Det=abs(np.linalg.det(product)) print(Det) …

Total answers: 2

Change String to Float Python

Change String to Float Python Question: I would like to convert a list that I took out from a txt file into a float so I can make some calculous after in Python. I have the following list: [‘1,0,1.2’, ‘2,-1.5,1.2’, ‘3,-1.5,0’, ‘4,0,0’, ‘5,1.5,1.2’] And I would like it to look like this: [1,0,1.2,2,-1.5,1.2,3,-1.5,0,4,0,0,5,1.5,1.2] All of …

Total answers: 2

How to disable python rounding?

How to disable python rounding? Question: I’m looking for a way to disable this: print(0+1e-20) returns 1e-20, but print(1+1e-20) returns 1.0 I want it to return something like 1+1e-20. I need it because of this problem: from numpy import sqrt def f1(x): return 1/((x+1)*sqrt(x)) def f2(x): return 1/((x+2)*sqrt(x+1)) def f3(x): return f2(x-1) print(f1(1e-6)) print(f3(1e-6)) print(f1(1e-20)) …

Total answers: 2

Base64 Binary String representation to float convertion

Base64 Binary String representation to float convertion Question: I did a lot of research and can’t seem to find the answer for this, e.g: link1 link2 So I have this base64 encoded string: BzM=, and I know that the correct value is 18.43 This value comes from a decimal field from a MySql database with …

Total answers: 1

How to convert voltage (or frequency) floating number read backs to mV (or kHz)?

How to convert voltage (or frequency) floating number read backs to mV (or kHz)? Question: I am successfully able to read back data from an instrument: When the read back is a voltage, I typically read back values such as 5.34e-02 Volts. When the read back is frequency, I typically read values like 2.95e+04or 1.49e+05 …

Total answers: 2

infinite while loop although I put break statement

infinite while loop although I put break statement Question: this code is supposed to take slope (m) and y-intercept (b) of two lines and checks if these two line hit each other or not. the problem is my while loop is infinite although I have condition and break statement print("enter the first m: ") m …

Total answers: 5

Not able to 'numpy.float64' to int in python

Not able to 'numpy.float64' to int in python Question: I am trying to convert ‘numpy.float64’ to float to do this: ax.get_ylim()[0].astype(float) Which prints out -2.25 But when I check with type(ax.get_ylim()[0].astype(float)) it is printing out numpy.float64. Why is it not changing the data type? Asked By: Slartibartfast || Source Answers: Can you try the following: …

Total answers: 2

How to fix the number of decimals to 500 digits output in Python?

How to fix the number of decimals to 500 digits output in Python? Question: In the following example: import math x = math.log(2) print("{:.500f}".format(x)) I tried to get 500 digits output I get only 53 decimals output of ln(2) as follows: 0.69314718055994528622676398299518041312694549560546875000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 How I can fix this problem? Asked By: Psylife || Source Answers: You …

Total answers: 3

Applying Decimal in Python

Applying Decimal in Python Question: I am trying to solve the following question: Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies? Old attempt with reference …

Total answers: 2