floating-point

Reading floats from file with python

你离开我真会死。 提交于 2021-01-27 06:32:35
问题 My input file has the form: 5.0, 1000.0, 100000000000000.0, 115.2712, 230.538, 345.796, 461.0408, 1.053E-09, 1.839E-09, 1.632E-10, 1.959E-12, 4.109, 3.683, 3.586, 3.650 where every number is essentially in a line. What I want to do is read all the floats, and then append only columns 7 through 10 to an array. Here's what I've written: T=[] with open("test.txt", "r") as file1: for line in file1.readlines(): f_list = [float(i) for i in line.split(",")] T.append(float(f_list[7])) T.append(float

JavaScript and Dealing with Floating Point Determinism

百般思念 提交于 2021-01-27 05:40:49
问题 I'm looking to build a browser multiplayer game using rollback netcode that runs a deterministic simulation on the clients. I prototyped the netcode in Flash already before I ran into the floating point roadblock. Basically, from what I understand, integer math in Flash is done by casting int s to Number s, doing the math, then casting back to int . It's faster apparently, but it means that there's no chance of deterministic math across different computer architectures. Before I dump all my

JavaScript and Dealing with Floating Point Determinism

自闭症网瘾萝莉.ら 提交于 2021-01-27 05:39:17
问题 I'm looking to build a browser multiplayer game using rollback netcode that runs a deterministic simulation on the clients. I prototyped the netcode in Flash already before I ran into the floating point roadblock. Basically, from what I understand, integer math in Flash is done by casting int s to Number s, doing the math, then casting back to int . It's faster apparently, but it means that there's no chance of deterministic math across different computer architectures. Before I dump all my

Compile-time (constexpr) float modulo?

回眸只為那壹抹淺笑 提交于 2021-01-27 05:35:19
问题 Consider the following function that computes the integral or floating-point modulo depending on the argument type, at compile-time: template<typename T> constexpr T modulo(const T x, const T y) { return (std::is_floating_point<T>::value) ? (x < T() ? T(-1) : T(1))*((x < T() ? -x : x)-static_cast<long long int>((x/y < T() ? -x/y : x/y))*(y < T() ? -y : y)) : (static_cast<typename std::conditional<std::is_floating_point<T>::value, int, T>::type>(x) %static_cast<typename std::conditional<std:

How to compute the average of doubles, so that the total error is minimal?

£可爱£侵袭症+ 提交于 2021-01-27 05:20:38
问题 Assume we have a long array of doubles, say, N == 1000000 . array<double, N> arr; There are two naive approaches to compute the average. First double result = 0; for (double x : arr) { result += x; } result /= arr.size(); This may be inaccurate when the sum of values is very big. Floating point numbers lose precision then. Another approach is: double result = 0; for (double x : arr) { result += x / arr.size(); } This may lose precision when the numbers are small. Is there any fail-safe way to

C - Printing out float values

老子叫甜甜 提交于 2021-01-27 04:54:55
问题 I have a C++ program that takes in values and prints out values like this: getline(in,number); cout << setw(10) << number << endl; I have an equivalent C program that takes in values and prints out like so: fscanf(rhs, "%e", &number); printf("%lf\n", number); But while the C++ program prints out, 0.30951 the C program prints out 0.309510 . More examples: C++: 0.0956439 C: 0.095644 . It seems to print the same results as long as the value is 7 digits long, but if its shorter the 7 digits, it

C - Printing out float values

风格不统一 提交于 2021-01-27 04:54:09
问题 I have a C++ program that takes in values and prints out values like this: getline(in,number); cout << setw(10) << number << endl; I have an equivalent C program that takes in values and prints out like so: fscanf(rhs, "%e", &number); printf("%lf\n", number); But while the C++ program prints out, 0.30951 the C program prints out 0.309510 . More examples: C++: 0.0956439 C: 0.095644 . It seems to print the same results as long as the value is 7 digits long, but if its shorter the 7 digits, it

Is there any way to get correct rounding with the i387 fsqrt instruction?

天涯浪子 提交于 2021-01-27 02:35:48
问题 Is there any way to get correct rounding with the i387 fsqrt instruction?... ... aside from changing the precision mode in the x87 control word - I know that's possible, but it's not a reasonable solution because it has nasty reentrancy-type issues where the precision mode will be wrong if the sqrt operation is interrupted. The issue I'm dealing with is as follows: the x87 fsqrt opcode performs a correctly-rounded (per IEEE 754) square root operation in the precision of the fpu registers,

Is there any way to get correct rounding with the i387 fsqrt instruction?

佐手、 提交于 2021-01-27 02:34:32
问题 Is there any way to get correct rounding with the i387 fsqrt instruction?... ... aside from changing the precision mode in the x87 control word - I know that's possible, but it's not a reasonable solution because it has nasty reentrancy-type issues where the precision mode will be wrong if the sqrt operation is interrupted. The issue I'm dealing with is as follows: the x87 fsqrt opcode performs a correctly-rounded (per IEEE 754) square root operation in the precision of the fpu registers,

Floating point accuracy with different languages

送分小仙女□ 提交于 2021-01-26 19:15:35
问题 I'm currently doing distance calculations between coordinates and have been getting slightly different results depending on the language used. Part of the calculation is taking calculating the cosine of a given radian . I get the following results // cos(0.8941658257446736) // 0.6261694290123146 node // 0.6261694290123146 rust // 0.6261694290123148 go // 0.6261694290123148 python // 0.6261694290123148 swift // 0.6261694290123146 c++ // 0.6261694290123146 java // 0.6261694290123147 c I would