floating-point

How do I parse a float from a string that might contain left-over characters without doing manual parsing?

佐手、 提交于 2021-01-29 21:17:34
问题 How do I parse a float from a string that may contain left-over characters, and figure out where it ends, without doing manual parsing (e.g. parseFloat() in JS)? An example would be the string "2e-1!" . I want to evaluate the next float from the string such that I can split this string into "2e-1" and "!" (for example, if I wanted to implement a calculator.) How would I do this without writing a parser to see where the float ends, taking that as an &str , and then using .parse() ? I am aware

Multiplication issue: dividing and multiplying by the same decimal does not return original number [duplicate]

喜夏-厌秋 提交于 2021-01-29 20:42:09
问题 This question already has answers here : Ruby float precision (2 answers) Closed 12 months ago . I am aware of the Floating Point precision issues on multiple languages, but I thought I was only going to encounter those issues when multiplying very small or big numbers. This simple math is incorrect (byebug) 30*36.3/36.3 30.000000000000004 Why is this happening and what is the suggested way around it? I don't want to have to use the .to_i function since not always I will be dealing with

Float is not converting to integer pandas

纵然是瞬间 提交于 2021-01-29 11:17:58
问题 I used this code to convert my float numbers into an integer, however, it does not work. Here are all step I gone through so far: Step 1: I converted timestamp1 and timestamp2 to datetime in order subtract and get days: a=pd.to_datetime(df['timestamp1'], format='%Y-%m-%dT%H:%M:%SZ') b=pd.to_datetime(df['timestamp2'], format='%Y-%m-%dT%H:%M:%SZ') df['delta'] = (b-a).dt.days Step 2: Converted the strings into integers as the day: df['delta'] = pd.to_datetime(df['delta'], format='%Y-%m-%d',

How to emulate float/double/real in Bazel?

被刻印的时光 ゝ 提交于 2021-01-29 09:45:37
问题 Bazel (0.26.0) does not support float types as you can read here. However, I would like to compute some magic (floating-point) number store them in string as shown here: def magicNumber(): fileStr = "" count = 200 for i in range(0, count-1): v = i / count fileStr += str(v) + " " return fileStr I want to use Bazel-only features to achieve this. It is clear to me that I could also place my computation for instance in batch/shell script, but I want stick to Bazel-only features. Any ideas how to

Convert IEEE float to TI TMS320C30 32bits float in python

穿精又带淫゛_ 提交于 2021-01-29 07:50:52
问题 I need to convert a python float to a TI DSP TMS320C30 float representation, following this convention: http://www.ti.com/lit/an/spra400/spra400.pdf#page=13 I've tried a few things, but I can't seem to wrap my head around the proposed algorithm. I also found a C version of the algorithm, but it looks like it is a version that runs in the TI DSP, so there are operations that I can't figure out (reverse the sign, for instance). I have a very naive implementation below, but it doesn't work... #

Constrain numpy to automatically convert integers to floating-point numbers (python 3.7)

感情迁移 提交于 2021-01-29 06:17:29
问题 I have just made the following mistake: a = np.array([0,3,2, 1]) a[0] = .001 I was expecting 0 to be replaced by .001 (and the dtype of my numpy array to automatically switch from int to float). However, print (a) returns: array([0, 3, 2, 1]) Can somebody explain why numpy is doing that? I am confused because multiplying my array of integers by a floating point number will automatically change dtype to float: b = a*.1 print (b) array([0. , 0.3, 0.2, 0.1]) Is there a way to constrain numpy to

float identity comparison in Python lambda function

╄→尐↘猪︶ㄣ 提交于 2021-01-28 12:14:15
问题 Why does the following happen with Python's lambdas (in both Python 2 and 3)? >>> zero = lambda n: n is 0 >>> zero(0) True >>> zero = lambda n: n is 0.0 >>> zero(0.0) False 回答1: The most common Python implementation store a number of small integers as "constant" or "permanent" objects in a pre-allocated array: see the documentation. So, these numbers can be recongized as identical objects using the is operator. This is not done for floats. If you were to compare the numbers using the equality

Prevent underflow in floating point division in Python

扶醉桌前 提交于 2021-01-28 08:23:52
问题 Suppose both x and y are very small numbers, but I know that the true value of x / y is reasonable. What is the best way to compute x/y ? In particular, I have been doing np.exp(np.log(x) - np.log(y) instead, but I'm not sure if that would make a difference at all? 回答1: Python uses the floating-point features of the hardware it runs on, according to Python documentation. On most common machines today, that is IEEE-754 arithmetic or something near it. That Python documentation is not explicit

How to divide large numbers with a floating point?

ⅰ亾dé卋堺 提交于 2021-01-28 04:53:06
问题 I have some large numbers (like 10000000 digits in total or more ). Since I will lose precision, they are stored like strings . Lets take some 'small' numbers as an example (some numbers also can be integers): a = 4782916578987656789087654678908765467417657489104.9486718490129876578 b = 657890876566567890.8765467890987654 I want to make some operations on them. Division . Well, answer is 7.270075858095143e+30 Multiplicaiton: 3.1466371806949598e+66 And so on. Desirable output is full number as

Exponentiation in Fortran/gfortran to high precision

时光总嘲笑我的痴心妄想 提交于 2021-01-28 04:45:34
问题 How does gfortran handle exponentiation with a integer vs a real? I always assumed it was the same, but consider the example: program main implicit none integer, parameter :: dp = selected_real_kind(33,4931) real(kind=dp) :: x = 82.4754500815524510_dp print *, x print *, x**4 print *, x**4.0_dp end program main Compiling with gfortran gives 82.4754500815524510000000000000000003 46269923.0191143410452125643548442147 46269923.0191143410452125643548442211 Now clearly these numbers almost agree -