double

Java: Check if two double values match on specific no of decimal places

北慕城南 提交于 2019-12-22 04:49:20
问题 Comparing those two values shall result in a "true": 53.9173333333333 53.9173 回答1: If you want a = 1.00001 and b = 0.99999 be identified as equal: return Math.abs(a - b) < 1e-4; Otherwise, if you want a = 1.00010 and b = 1.00019 be identified as equal, and both a and b are positive and not huge: return Math.floor(a * 10000) == Math.floor(b * 10000); // compare by == is fine here because both sides are integral values. // double can represent integral values below 2**53 exactly. Otherwise, use

Can float (or double) be set to NaN?

时光毁灭记忆、已成空白 提交于 2019-12-22 01:33:35
问题 Note: Similar to Can an integer be NaN in C++? I understand this has little practical purpose, but can a float or double be set to NaN ? 回答1: The Float object contains a static value, which is a float type, called NaN . So float myFloat = Float.NaN; gives you what you are asking. http://download.oracle.com/javase/6/docs/api/java/lang/Float.html#NaN 回答2: Sure! NaN is a static constant in the Float and Double classes. double x = Double.NaN; 回答3: Yes float f = Float.NaN; See the doc for more

Returning double with precision

老子叫甜甜 提交于 2019-12-21 23:37:28
问题 Say I have a method returning a double , but I want to determine the precision after the dot of the value to be returned. I don't know the value of the double varaible. Example: double i = 3.365737; return i; I want the return value to be with precision of 3 number after the dot Meaning: the return value is 3.365 . Another example: double i = 4644.322345; return i; I want the return value to be: 4644.322 回答1: What you want is truncation of decimal digits after a certain digit. You can easily

Double precision in C - printing 50 significant figures yields inaccurate values

删除回忆录丶 提交于 2019-12-21 19:57:01
问题 I'm doing an integration program with Riemann sums for my Calculus class. I've decided to use C when computing my integrals, and I noticed a huge error in my program that derives from this problem. #include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char** argv) { double x = 2.0/20.0; printf("%1.50f \n", x); return (EXIT_SUCCESS); } The program gives me : 0.10000000000000000555111512312578270211815834045410. My question: Why does this happen? And how can I fix this? Or

Dump hex float in C++

て烟熏妆下的殇ゞ 提交于 2019-12-21 15:19:15
问题 I tried following: std::cout << std::hex << 17.0625; But it dumped it in decimal. I'd like to see 11.01 (17.0625 in hex). How can I print some floating point value in hex? Please do not offer solutions like: void outhexdigits(std::ostream& out, fp_t d, int max_chars=160) { while(d > 0. && max_chars) { while(d < 1. && max_chars){ out << '0'; --max_chars; d*=16; } if (d>=1. && max_chars) { int i = 0; while (d>=1.) ++i, --d; out << std::hex << i; --max_chars; } } } Is there any way to dump float

How to send multiple textures to a fragment shader in WebGL?

家住魔仙堡 提交于 2019-12-21 13:11:22
问题 So in the javascript portion of my code, here is the snippet that actually sends an array of pixels to the vertex and fragment shaders- but I am only working with 1 texture when I get to those shaders- is there anyway that I can send two textures at a time? if so, how would I 'catch' both of them on the GLSL side of the codee? if (it > 0){ gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, texture); gl.activeTexture(gl.TEXTURE0); gl.bindFramebuffer(gl.FRAMEBUFFER, FBO2);} else{ gl

How to alter double by its smallest increment

六眼飞鱼酱① 提交于 2019-12-21 11:11:17
问题 Is something broken or I fail to understand what is happening? static String getRealBinary(double val) { long tmp = Double.doubleToLongBits(val); StringBuilder sb = new StringBuilder(); for (long n = 64; --n > 0; tmp >>= 1) if ((tmp & 1) == 0) sb.insert(0, ('0')); else sb.insert(0, ('1')); sb.insert(0, '[').insert(2, "] [").insert(16, "] [").append(']'); return sb.toString(); } public static void main(String[] argv) { for (int j = 3; --j >= 0;) { double d = j; for (int i = 3; --i >= 0;) { d +

Converting Double to NSNumber in Swift Loses Accuracy [duplicate]

社会主义新天地 提交于 2019-12-21 07:56:37
问题 This question already has answers here : Is floating point math broken? (31 answers) Closed 3 years ago . For some reason, certain Doubles in my Swift app are giving me trouble when converting to NSNumber, while some are not. My app needs to convert doubles with 2 decimal places (prices) to NSNumbers so they can be stored and retrieved using Core Data. For example, a few particular prices such as 79.99 would evaluate to 99.98999999999999 unless specifically formatted using NSNumber's

Convert string to double with 2 digit after decimal separator

六月ゝ 毕业季﹏ 提交于 2019-12-21 05:14:22
问题 All began with these simple lines of code: string s = "16.9"; double d = Convert.ToDouble(s); d*=100; The result should be 1690.0, but it's not. d is equal to 1689.9999999999998. All I want to do is to round a double to value with 2 digit after decimal separator. Here is my function. private double RoundFloat(double Value) { float sign = (Value < 0) ? -0.01f : 0.01f; if (Math.Abs(Value) < 0.00001) Value = 0; string SVal = Value.ToString(); string DecimalSeparator = System.Globalization

double to hex string & hex string to double

蹲街弑〆低调 提交于 2019-12-21 04:40:33
问题 What I'm trying to do is to convert a double to hex string and then back to double. The following code does conversion double-to-hex string. char * double2HexString(double a) { char *buf = new char[17]; // double is 8-byte long, so we have 2*8 + terminating \0 char *d2c; d2c = (char *) &a; char *n = buf; int i; for(i = 0; i < 8; i++) { sprintf(n, "%02X", *d2c++); n += 2; } *(n) = '\0'; } This seems work, however, I'm not sure how to convert the resulting string back to double. Please advise :