double-precision

NaN or false as double precision return value

梦想与她 提交于 2021-01-29 18:27:22
问题 I have a function that returns a double value. In some cases the result is zero, and this results should be handled in the caller routing accordingly. I was wondering what is the proper way of returning zero (NaN, false, etc!) in place of double value number: double foo(){ if (some_conditions) { return result_of_calculations; } else { // Which of the following is better? return std::numeric_limits<double>::quiet_NaN(); // (1) return 0; // (2) return false; // (3) return (int) 0; // (4) } }

Error due to limited precision of float and double

做~自己de王妃 提交于 2020-01-13 17:46:27
问题 In C++, I use the following code to work out the order of magnitude of the error due to the limited precision of float and double: float n=1; float dec = 1; while(n!=(n-dec)) { dec = dec/10; } cout << dec << endl; (in the double case all I do is exchange float with double in line 1 and 2) Now when I compile and run this using g++ on a Unix system, the results are Float 10^-8 Double 10^-17 However, when I compile and run it using MinGW on Windows 7, the results are Float 10^-20 Double 10^-20

IEEE-754 floating-point precision: How much error is allowed?

*爱你&永不变心* 提交于 2020-01-09 08:01:30
问题 I'm working on porting the sqrt function (for 64-bit doubles) from fdlibm to a model-checker tool I'm using at the moment (cbmc). As part of my doings, I read a lot about the ieee-754 standard, but I think I didn't understand the guarantees of precision for the basic operations (incl. sqrt). Testing my port of fdlibm's sqrt, I got the following calculation with sqrt on a 64-bit double: sqrt

IEEE-754 floating-point precision: How much error is allowed?

与世无争的帅哥 提交于 2020-01-09 08:01:07
问题 I'm working on porting the sqrt function (for 64-bit doubles) from fdlibm to a model-checker tool I'm using at the moment (cbmc). As part of my doings, I read a lot about the ieee-754 standard, but I think I didn't understand the guarantees of precision for the basic operations (incl. sqrt). Testing my port of fdlibm's sqrt, I got the following calculation with sqrt on a 64-bit double: sqrt

infinity as result in double operation

醉酒当歌 提交于 2019-12-24 15:29:43
问题 I would understand why the result is infinity. I write the code below and I always receive inf as result. There is any precision problem with my code? #include <stdio.h> #include <stdlib.h> #include "cuda.h" #include "curand_kernel.h" #define NDIM 30 #define NPAR 5 #define DIMPAR NDIM*NPAR __device__ double uniform(int index){ return (double) 0.767341; } __global__ void iteracao(double *pos){ int thread = threadIdx.x + blockDim.x * blockIdx.x; double tvel; int i = 0; double l, r, t; if(thread

Storing numbers with higher precision in C

我们两清 提交于 2019-12-24 05:41:12
问题 I am writing a program in which I need to store numbers with a very high precision(around 10^-10 ) and then further use them a parameter( create_bloomfilter ([yet to decide the type] falsePositivity, long expected_num_of_elem) ). The highest precision I am able to get is with double (something around 10^-6 ) which is not sufficient. How can we store numbers with more higher precision in c? 回答1: You have been misinformed about double . The smallest positive number you can store in a double is

Order of magnitude for double precision

不羁的心 提交于 2019-12-24 04:58:50
问题 What order of magnitude difference should I be expecting for a subtraction between two theoretically equal double precision numbers? I have two double precision arrays. They are expected to be theoretically same. They are both calculated by two completely different methodologies, so there is some numerical difference between them. I checked them element by element and my maximum difference is coming out to be 6.5557799910909154E-008. My boss says that for a double precision this is a very

What does the “double” do in ceil(double)?

白昼怎懂夜的黑 提交于 2019-12-23 12:49:23
问题 I have a number (let's say, 34), and I want to find its next multiple of ten. I can do this by: Dividing the number by 10 Rounding it up to a whole number Multiplying by 10. After a bit of research, I discovered that this is the code for that in Objective C: int number = 34; int roundedNumber = ceil((double)number/10)*10; My question is: what is the (double) for, and why does removing (double) cause it to round down instead of up? I understand from googling that changes the float format to

Tiny numbers in place of zero?

…衆ロ難τιáo~ 提交于 2019-12-23 09:47:31
问题 I have been making a matrix class (as a learning exercise) and I have come across and issue whilst testing my inverse function. I input a arbitrary matrix as such: 2 1 1 1 2 1 1 1 2 And got it to calculate the inverse and I got the correct result: 0.75 -0.25 -0.25 -0.25 0.75 -0.25 -0.25 -0.25 0.75 But when I tried multiplying the two together to make sure I got the identity matrix I get: 1 5.5111512e-017 0 0 1 0 -1.11022302e-0.16 0 1 Why am I getting these results? I would understand if I was

Why is this bearing calculation so inacurate?

和自甴很熟 提交于 2019-12-21 08:22:00
问题 Is it even that inaccurate? I re-implented the whole thing with Apfloat arbitrary precision and it made no difference which I should have known to start with!! public static double bearing(LatLng latLng1, LatLng latLng2) { double deltaLong = toRadians(latLng2.longitude - latLng1.longitude); double lat1 = toRadians(latLng1.latitude); double lat2 = toRadians(latLng2.latitude); double y = sin(deltaLong) * cos(lat2); double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLong);