I found myself needing to compute the “integer cube root”, meaning the cube root of an integer, rounded down to the nearest integer. In Python, we could use the NumPy floating-point cbrt() function: Though this works most of the time, it fails at certain input x, with the result being one less than expected. For example, icbrt(15**3) == 14, which
Tag: floating-point
How many times can you divide 24**36 by 2 until it becomes imprecise?
How many times can you divide 24**36 by 2 until it becomes imprecise? I had this question on one of my quizzes in a python course, and I’m curious as to what the correct answer is. (It’s already submitted so no cheating here) I wrote this small bit of code in python to calculate how many times you can evenly
How to disable python rounding?
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: returns f1 is the original function, f2 is f1 shifted by 1 to the left and f3 is f2 moved back by 1 to the right. By this logic, f1
Exctract a value from a Json file(python)
Hi i’m not an expert and this problem kept me stuck for such a long time I hope that someone here can help me i would like to exctract the value “interestExpense” from the following json file: In this case the result should be -130000000 as a string but i m trying to find a way to create an list(or
Python large numbers (float and integer)
I am trying to understand the following calculation results. Out[1], Out[2] and Out[3] seem to be related to the limit on precision of floats, and Out[4] may be due to the fact that there is no limit on digits of int. Correct? I wonder if someone can explain them in more detail. Answer To understand why this happens, we must
Trying to find averages from a .txt but I keep getting ValueError: could not convert string to float: ”
I’m using the txt file: https://drive.google.com/file/d/1-VrWf7aqiqvnshVQ964zYsqaqRkcUoL1/view?usp=sharin I’m running the script: And I ALWAYS get the error: ValueError: could not convert string to float: ” I’m pretty new-ish to Python and I’m really not sure what I’m doing wrong here. I’m trying to get averages for New York and San Francisco, then export the results AND the comparison to a txt
How is python’s float.__eq__ implemented in the language?
I know that the best way to compare two floats for equality is usually to use math.isclose(float_a, float_b). But I was curious to know how python does it if you simply do float_a == float_b. I suppose it’s implemented in C, but what is the logic behind it ? Answer Here is the source code for float object comparisons Essentially.
Multiple data types in a python list
I have a list for birth data each record has 3 columns for [DOB, weight, height] as such: I need to change the data types of this as they are all strings in the list I want the first item in the record to be datetime and then the rest to be floats. I have: I get an attribute error:
Why do I lose numerical precision when extracting element from list in python?
I have a pandas dataframe that looks like this: I am trying to extract the 1st element from the Series of lists using this code: and I get this result: Why do I lose precision and what can I do to keep it? Answer By default, pandas displays floating-point values with 6 digits of precision. You can control the precision
It doesn’t append as floating Python | Problem #18
I’m writing this code for the problem #18 in Codeabbey. I need to calculate the square root of an array of numbers [150, 0, 5, 1 10, 3] I have to divide this array in three arrays (x,n) [[150, 0], [5, 1], [10 3]] where x: is the number I want to calculate the square root of and n: is