floating-point

How do calculators work with precision?

拥有回忆 提交于 2021-01-02 05:48:21
问题 I wonder how calculators work with precision. For example the value of sin(M_PI) is not exactly zero when computed in double precision: #include <math.h> #include <stdio.h> int main() { double x = sin(M_PI); printf("%.20f\n", x); // 0.00000000000000012246 return 0; } Now I would certainly want to print zero when user enters sin(π). I can easily round somewhere on 1e–15 to make this particular case work, but that’s a hack, not a solution. When I start to round like this and the user enters

How to refine the result of a floating point division result?

﹥>﹥吖頭↗ 提交于 2021-01-02 04:00:43
问题 I have an an algorithm for calculating the floating point square root divide using the newton-raphson algorith. My results are not fully accurate and sometimes off by 1 ulp. I was wondering if there is a refinement algorithm for floating point division to get the final bits of accuracy. I use the tuckerman test for square root, but is there a similar algorithm for division? Or can the tuckerman test be adapted for division? I tried using this algorithm too but didn't get full accuracy results

How to refine the result of a floating point division result?

自作多情 提交于 2021-01-02 03:59:36
问题 I have an an algorithm for calculating the floating point square root divide using the newton-raphson algorith. My results are not fully accurate and sometimes off by 1 ulp. I was wondering if there is a refinement algorithm for floating point division to get the final bits of accuracy. I use the tuckerman test for square root, but is there a similar algorithm for division? Or can the tuckerman test be adapted for division? I tried using this algorithm too but didn't get full accuracy results

PowerShell Round & Format Float to max 2 decimals?

最后都变了- 提交于 2020-12-30 04:56:24
问题 I found lots of stuff to format floats to common known numbers, but how can I format a float to a max of 2 decimals, but only if the decimals are needed? Examples: 1.11 # not 1.111 1.12 # it was 1.116 (round up) 1.1 # not 1.10 1 # not 1.00 if I do $('{0:N2}' -f $flt) I get 1.00 # :( Thanks in advance! 回答1: Use [math]::round , ie: [math]::round(1.111,2) will return 1.11 and [math]::round(1.00,2) yields 1 回答2: You can use the # character in a custom numeric format string to include non-zero

PowerShell Round & Format Float to max 2 decimals?

删除回忆录丶 提交于 2020-12-30 04:56:21
问题 I found lots of stuff to format floats to common known numbers, but how can I format a float to a max of 2 decimals, but only if the decimals are needed? Examples: 1.11 # not 1.111 1.12 # it was 1.116 (round up) 1.1 # not 1.10 1 # not 1.00 if I do $('{0:N2}' -f $flt) I get 1.00 # :( Thanks in advance! 回答1: Use [math]::round , ie: [math]::round(1.111,2) will return 1.11 and [math]::round(1.00,2) yields 1 回答2: You can use the # character in a custom numeric format string to include non-zero

Python: return float 1.0 as int 1 but float 1.5 as float 1.5 [duplicate]

跟風遠走 提交于 2020-12-29 08:54:55
问题 This question already has answers here : How to check if a float value is a whole number (13 answers) How to make python print 1 as opposed to 1.0 (3 answers) Closed last year . In Python is there a way to turn 1.0 into a integer 1 while the same function ignores 1.5 and leaves it as a float ? Right now, int() will turn 1.0 into 1 but it will also round 1.5 down to 1 , which is not what I want. 回答1: Continuing from the comments above: Using is_integer(): Example from the docs : >>> (1.5).is

Fastest C++ way to convert float to string

随声附和 提交于 2020-12-29 06:21:08
问题 I have encountered problem of converting float to string where to_string is too slow for me as my data might involves few millions floats. I already have solution on how to write those data out fast. However, after solving that problem, I soon realized that the conversion of float to string is leaving a big impact. So, is there any ideas or solution for this other than using other non standard library? 回答1: An optimization that comes in mind is to not directly use to_string, which creates a

Fastest C++ way to convert float to string

纵饮孤独 提交于 2020-12-29 06:20:47
问题 I have encountered problem of converting float to string where to_string is too slow for me as my data might involves few millions floats. I already have solution on how to write those data out fast. However, after solving that problem, I soon realized that the conversion of float to string is leaving a big impact. So, is there any ideas or solution for this other than using other non standard library? 回答1: An optimization that comes in mind is to not directly use to_string, which creates a

Bizarre floating-point behavior with vs. without extra variables, why?

允我心安 提交于 2020-12-26 06:28:40
问题 When I run the following code in VC++ 2013 (32-bit, no optimizations): #include <cmath> #include <iostream> #include <limits> double mulpow10(double const value, int const pow10) { static double const table[] = { 1E+000, 1E+001, 1E+002, 1E+003, 1E+004, 1E+005, 1E+006, 1E+007, 1E+008, 1E+009, 1E+010, 1E+011, 1E+012, 1E+013, 1E+014, 1E+015, 1E+016, 1E+017, 1E+018, 1E+019, }; return pow10 < 0 ? value / table[-pow10] : value * table[+pow10]; } int main(void) { double d = 9710908999.008999; int j

Ruby float precision

你说的曾经没有我的故事 提交于 2020-12-25 01:00:34
问题 As I understand it, Ruby (1.9.2) floats have a precision of 15 decimal digits. Therefore, I would expect rounding float x to 15 decimal places would equal x . For this calculation this isn't the case. x = (0.33 * 10) x == x.round(15) # => false Incidentally, rounding to 16 places returns true. Can you please explain this to me? 回答1: Part of the problem is that 0.33 does not have an exact representation in the underlying format, because it cannot be expressed by a series of 1 / 2 n terms. So,