integer

Float versus Integer arithmetic performance on modern chips

眉间皱痕 提交于 2019-12-23 16:13:23
问题 Consider a Viterbi decoder on an additive model. It spends its time doing additions and comparisons. Now, consider two: one with C/C++ float as the data type, and another with int . On modern chips, would you expect int to run significantly faster than float ? Or will the wonders of pipelining (and the absence of multiplication and division) make it all come out about even? 回答1: Depends on what you mean by significantly . I usually expect to see ints perform about 2x faster, but it all

How can I determine if scanf read what was specified in format?

浪尽此生 提交于 2019-12-23 15:25:45
问题 I have a program that defines a variable int data The program uses scanf("%d",&data) to read data from stdin. If the data from stdin is not an integer, I have to print error message. I tried if(scanf("%d",&data) ==EOF){ printf("error");return 1;} It didn`t works for me. So, how can I determine if scanf failed or succeeded? 回答1: scanf 's return value is an integer, telling you how many items were succesfully read. If your single integer was read successfully, scanf will return 1. e.g. int

PHP array: integer index vs string index

萝らか妹 提交于 2019-12-23 13:44:10
问题 Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array )? So for example, what are the differences between the following two arrays: $intIndex[5] = "Hello"; $intIndex[6] = "World"; $intIndex[7] = "!"; And $strIndex['5'] = "Hello"; $strIndex['6'] = "World"; $strIndex['7'] = "!"; In the first case, what happens to $intIndex[0] to $intIndex[4] ? 回答1: From the manual (emphasis mine): The key can either be

PHP array: integer index vs string index

夙愿已清 提交于 2019-12-23 13:44:09
问题 Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array )? So for example, what are the differences between the following two arrays: $intIndex[5] = "Hello"; $intIndex[6] = "World"; $intIndex[7] = "!"; And $strIndex['5'] = "Hello"; $strIndex['6'] = "World"; $strIndex['7'] = "!"; In the first case, what happens to $intIndex[0] to $intIndex[4] ? 回答1: From the manual (emphasis mine): The key can either be

Dynamic Java integer/long overflow checking versus performance

拟墨画扇 提交于 2019-12-23 12:57:32
问题 This is a rather theoretical question, so while the language is specifically Java, any general solution will suffice. Suppose I wanted to write a trivial factorial function: long factorial(int n) { //handle special cases like negatives, etc. long p = 1; for(int i = 1; i <= n; i++) { p = p * n; } return p; } But now, I also want to check if the factorial overflows (without simply hard coding a MAX_FACTORIAL_PARAMETER or something of the like). In general checking for overflow during

matrix multiplication for integral types using BLAS

十年热恋 提交于 2019-12-23 12:56:30
问题 Is there an equivalent of dgemm (from BLAS) for integral types? I only know of dgemm, sgemm for double precision / single precision matrices, but would like to have it for matrices that are of integral type such as int (or short int...). Note: I'm not looking for a solution that involves converting to float/double, and am looking for a fast library implementation. Also, same question for dgemms (using strassen algorithm). 回答1: BLAS algorithms don't natively support integer types. 回答2: You did

How does Integer.toString() works internally?

梦想的初衷 提交于 2019-12-23 12:52:58
问题 I found that a similar question has been asked before here : how does Float.toString() and Integer.toString() works? But this doesn't speak about how that function internally works. When I opened the internally source code of Integer.toString() , it is not understandable for normal junior java programmer. Can somebody please explain what happens internally in short description ? NOTE : This was one of the interview questions that I was asked recently. I had no idea about how to answer such

Regex to find integers and decimals in string

懵懂的女人 提交于 2019-12-23 12:33:29
问题 I have a string like: $str1 = "12 ounces"; $str2 = "1.5 ounces chopped; I'd like to get the amount from the string whether it is a decimal or not (12 or 1.5), and then grab the immediately preceding measurement (ounces). I was able to use a pretty rudimentary regex to grab the measurement, but getting the decimal/integer has been giving me problems. Thanks for your help! 回答1: If you just want to grab the data, you can just use a loose regex: ([\d.]+)\s+(\S+) ([\d.]+) : [\d.]+ will match a

Purpose of bitwise OR of an integer with its negative

瘦欲@ 提交于 2019-12-23 12:25:31
问题 I was curious about the implementation and representation of NaN in both IEEE single- and double-precision floating points, and I found this implementation of an "is NaN" function. Namely: int isnan(double x) { int32_t hx,lx; // Move lower 32 bits of double to lx, higher 32 to hx. EXTRACT_WORDS(hx,lx,x); // Remove sign bit, since -NaN and NaN are both NaN. hx &= 0x7fffffff; // Equivalent to hx |= (lx != 0). hx |= (u_int32_t)(lx|(-lx))>>31; // Difference is negative iff (hx & 0x7ff00000) ==

Normalized Integer to/from Float Conversion

喜你入骨 提交于 2019-12-23 12:24:30
问题 I need to convert normalized integer values to and from real floating-point values. For instance, for int16_t, a value of 1.0 is represented by 32767 and -1.0 is represented by -32768. Although it's a bit tedious to do this for each integer type, both signed and unsigned, it's still easy enough to write by hand. However, I want to use standard methods whenever possible rather than going off and reinventing the wheel, so what I'm looking for is something like a standard C or C++ header, a