floating-point

Reading a file with Fortran formatted small floats, using numpy

折月煮酒 提交于 2021-02-05 08:15:01
问题 I am trying to read a data file written by a Fortran program, in which every once in a while there is a very small float like 0.3299880-104 . The error message is: >np.loadtxt(filename, usecols = (1,)) File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 928, in loadtxt items = [conv(val) for (conv, val) in zip(converters, vals)] File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv return float(x) ValueError: invalid literal for

Reading a file with Fortran formatted small floats, using numpy

三世轮回 提交于 2021-02-05 08:11:20
问题 I am trying to read a data file written by a Fortran program, in which every once in a while there is a very small float like 0.3299880-104 . The error message is: >np.loadtxt(filename, usecols = (1,)) File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 928, in loadtxt items = [conv(val) for (conv, val) in zip(converters, vals)] File "/home/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 659, in floatconv return float(x) ValueError: invalid literal for

Why floating point registers are different than general purpose ones

筅森魡賤 提交于 2021-02-05 07:13:25
问题 Most architectures have different set of registers for storing regular integers and floating points. From a binary storage point of view, it shouldn't matter where things are stored right? it's just 1's and 0's, couldn't they pipe the same general purpose registers into floating point ALUs? SIMD ( xmm in x64) registers are capable of storing both Floating point and regular integers, so why doesn't the same concept apply to regular registers? 回答1: For practical processor design, there are a

correctly-rounded double-precision division

拟墨画扇 提交于 2021-02-05 06:46:26
问题 I am using the following algorithm for double-precision division and trying to make it correctly rounded in software emulation of floating-point. Let a be the dividend and b is the divisor. All operations are performed in Q2.62. Initial approximation to the reciprocal is . b/2 is the significand of b with its implicit bit added, and shifted one right. For what follows, when written a or b it is meant by the significand of a or b with its implicit bit added. The is approximated with

Float / Float = strange result

一个人想着一个人 提交于 2021-02-05 06:19:29
问题 I have two values, one from user input and another from DB. var userinput = form["someInput"]; var valuefromDB = GetValue(someNumber); public float? GetValue(int id){ return (float?) db.table.where(p=> p.id == id).select(p=> p.Value).SingleOrDefault(); } userinput have value "1" as string, while valuefromDB havevalue 0.001 as float. so 1 / 0.001 = 1000 but my c# code give me 999.999939 as result; var final = float.Parse(userinput) / valuefromDB when i have "2" as user input value, result is

How to create a `range`-like iterable object of floats?

一曲冷凌霜 提交于 2021-02-04 14:26:09
问题 I want to create a range -like construct in c++, that will be used like this: for (auto i: range(5,9)) cout << i << ' '; // prints 5 6 7 8 for (auto i: range(5.1,9.2)) cout << i << ' '; // prints 5.1 6.1 7.1 8.1 9.1 Handling the integer case is relatively easy: template<typename T> struct range { T from, to; range(T from, T to) : from(from), to(to) {} struct iterator { T current; T operator*() { return current; } iterator& operator++() { ++current; return *this; } bool operator==(const

How to create a `range`-like iterable object of floats?

一世执手 提交于 2021-02-04 14:25:24
问题 I want to create a range -like construct in c++, that will be used like this: for (auto i: range(5,9)) cout << i << ' '; // prints 5 6 7 8 for (auto i: range(5.1,9.2)) cout << i << ' '; // prints 5.1 6.1 7.1 8.1 9.1 Handling the integer case is relatively easy: template<typename T> struct range { T from, to; range(T from, T to) : from(from), to(to) {} struct iterator { T current; T operator*() { return current; } iterator& operator++() { ++current; return *this; } bool operator==(const

How to filter a list based on ascending values?

六月ゝ 毕业季﹏ 提交于 2021-02-04 07:29:21
问题 I have the following 3 lists: minimal_values = ['0,32', '0,35', '0,45'] maximal_values = ['0,78', '0,85', '0,72'] my_list = [ ['Morocco', 'Meat', '190,00', '0,15'], ['Morocco', 'Meat', '189,90', '0,32'], ['Morocco', 'Meat', '189,38', '0,44'], ['Morocco', 'Meat', '188,94', '0,60'], ['Morocco', 'Meat', '188,49', '0,78'], ['Morocco', 'Meat', '187,99', '0,70'], ['Spain', 'Meat', '190,76', '0,10'], ['Spain', 'Meat', '190,16', '0,20'], ['Spain', 'Meat', '189,56', '0,35'], ['Spain', 'Meat', '189,01'

How to filter a list based on ascending values?

一曲冷凌霜 提交于 2021-02-04 07:28:12
问题 I have the following 3 lists: minimal_values = ['0,32', '0,35', '0,45'] maximal_values = ['0,78', '0,85', '0,72'] my_list = [ ['Morocco', 'Meat', '190,00', '0,15'], ['Morocco', 'Meat', '189,90', '0,32'], ['Morocco', 'Meat', '189,38', '0,44'], ['Morocco', 'Meat', '188,94', '0,60'], ['Morocco', 'Meat', '188,49', '0,78'], ['Morocco', 'Meat', '187,99', '0,70'], ['Spain', 'Meat', '190,76', '0,10'], ['Spain', 'Meat', '190,16', '0,20'], ['Spain', 'Meat', '189,56', '0,35'], ['Spain', 'Meat', '189,01'

round up/down float to 2 decimals

一世执手 提交于 2021-02-04 06:28:46
问题 I would like to design a function f(x : float, up : bool) with these input/output: # 2 decimals part rounded up (up = True) f(142.452, True) = 142.46 f(142.449, True) = 142.45 # 2 decimals part rounded down (up = False) f(142.452, False) = 142.45 f(142.449, False) = 142.44 Now, I know about Python's round built-in function but it will always round 142.449 up, which is not what I want. Is there a way to do this in a nicer pythonic way than to do a bunch of float comparisons with epsilons