double

how can I extract the mantissa of a double

心已入冬 提交于 2019-12-17 09:48:07
问题 I would like to store in a variable the mantisssa of a double I have post a code to get the binary representation of a double : click here What should I change to isolate the mantissa 回答1: In <math.h> double frexp (double value, int *exp) decompose VALUE in exponent and mantissa. double ldexp (double value, int exp) does the reverse. To get an integer value, you have to multiply the result of frexp by FLT_RADIX exponent DBL_MANT_DIG (those are availble in <float.h> . To store that in an

C++ string to double conversion

偶尔善良 提交于 2019-12-17 09:42:08
问题 Usually when I write anything in C++ and I need to convert a char into an int I simply make a new int equal to the char. I used the code(snippet) string word; openfile >> word; double lol=word; I receive the error that Code1.cpp cannot convert `std::string' to `double' in initialization What does the error mean exactly? The first word is the number 50. Thanks :) 回答1: You can convert char to int and viceversa easily because for the machine an int and a char are the same, 8 bits, the only

printf rounding behavior for doubles

与世无争的帅哥 提交于 2019-12-17 09:40:15
问题 Can someone explain this behavior? I am well aware of machine-level representation of floating point numbers. This seems to be related to printf and its formats. Both numbers are represented exactly by floating-point notation (check: multiplying by 64 gives an integer). #include <stdio.h> #include <iostream> using namespace std; int main() { double x1=108.765625; printf("%34.30f\n", x1); printf("%9.5f\n", x1); printf("%34.30f\n", x1*64); double x2=108.046875; printf("%34.30lf\n", x2); printf(

Why is a round-trip conversion via a string not safe for a double?

大兔子大兔子 提交于 2019-12-17 08:53:43
问题 Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s = d1.ToString("R"); double d2 = double.Parse(s); bool s1 = d1 == d2; // -> s1 is False But according to MSDN: Standard Numeric Format Strings, the "R" option is supposed to guarantee round-trip safety. The round-trip ("R") format specifier is used to ensure that a numeric value that is converted to a string will be parsed back into the same

Double division behaving wrongly

夙愿已清 提交于 2019-12-17 07:54:18
问题 I have a HashMap called List<String, Intger> wordFreqMap whose size is 234 wordFreqMap = {radiology=1, shift=2, mummy=1, empirical=1, awful=1, geoff=1, .......} I want to calculate the term frequency of each word. term frequency = frequency of term / total number of terms public static Map<String, Double> getTFMap (Map<String, Integer> wordFreqMap) { Map<String, Double> tfMap = new HashMap<String, Double>(); int noOfTerms = wordFreqMap.size(); Double tf; for (Entry<String, Integer> word :

double or float, which is faster? [duplicate]

ぃ、小莉子 提交于 2019-12-17 06:37:48
问题 This question already has answers here : Is using double faster than float? (7 answers) Closed 5 years ago . I am reading "accelerated C++". I found one sentence which states "sometimes double is faster in execution than float in C++". After reading sentence I got confused about float and double working. Please explain this point to me. 回答1: Depends on what the native hardware does. If the hardware implements double (like the x86 does), then float is emulated by extending it there, and the

Compare double in VBA precision problem

谁都会走 提交于 2019-12-17 04:34:09
问题 I have trouble comparing 2 double in Excel VBA suppose that I have the following code Dim a as double Dim b as double a = 0.15 b = 0.01 After a few manipulations on b, b is now equal to 0.6 however the imprecision related to the double data type gives me headache because if a = b then //this will never trigger end if Do you know how I can remove the trailing imprecision on the double type? 回答1: You can't compare floating point values for equality. See this article on "Comparing floating point

In java, is it more efficient to use byte or short instead of int and float instead of double?

柔情痞子 提交于 2019-12-17 02:58:51
问题 This question was migrated from Computer Science Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . I've noticed I've always used int and doubles no matter how small or big the number needs to be. So in java, is it more efficient to use byte or short instead of int and float instead of double ? So assume I have a program with plenty of ints and doubles. Would it be worth going through and changing my ints to bytes or shorts if I knew the number would fit? I

How To Represent 0.1 In Floating Point Arithmetic And Decimal

旧时模样 提交于 2019-12-16 20:59:41
问题 I am trying to understand floating point arithmetic better and have seen a few links to 'What Every Computer Scientist Should Know About Floating Point Arithmetic'. I still don't understand how a number like 0.1 or 0.5 is stored in floats and as decimals. Can someone please explain how it is laid out is memory? I know about the float being two parts (i.e., a number to the power of something). 回答1: I've always pointed people towards Harald Schmidt's online converter, along with the Wikipedia

Integer division always zero [duplicate]

泪湿孤枕 提交于 2019-12-16 19:44:41
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: C programming division probably my question is very simple and stupid. I would like to store the value of a division, in particular 1 / x where x is a integer value. int x = 17; double result = 1/x; I try to do it but I always get 0.000000 ... I try to enter a value fixed in x, for example 1/17 but always get the same value .. what's Wrong? 回答1: You are doing integer division. Try the following and it will work