digits

How to customize the form of rounding

放肆的年华 提交于 2019-12-04 12:22:29
My question may seem simple, but still can not get something that works. I need to customize the Math.round rounding format or something to make it work as follows: If the number is 1.6 he should round to 1, if greater than or equal to 1.7 should round to 2.0 . And so to all other decimal results with # .6 The way I'm doing the 1.6 being rounded to 2 shall be rounded to 1. How can I do that? Thank you! Simply do this: double threshold = 0.7; Math.round(x - threshold + 0.5); You could write a method that takes a double variable as input and returns the integer based on the first digit after the

Difference in digits10 between GCC and MSVC

谁说胖子不能爱 提交于 2019-12-04 07:45:42
I have the following code: #include <iostream> #include <limits> int main() { std::cout << std::numeric_limits<unsigned long long>::digits10 << std::endl; return 0; } GCC 4.4 returns 19 MS VS 9.0 returns 18 Can someone please explain Why is there a difference between the two? I would have expected such a constant would be the same regardless of the compiler. If Visual C++ 2008 returns 18 for std::numeric_limits<unsigned long long>::digits10 , it is a bug (I don't have Visual C++ 2008 installed to verify the described behavior). In Visual C++ (at least for 32-bit and 64-bit Windows), unsigned

cv::Mat matrix, HOW TO Reduce digits to the right of the decimal point in cv::Mat?

依然范特西╮ 提交于 2019-12-04 07:42:50
I have an app that prints a 3x3 cv::Mat on the iPhone screen. I need to reduce the decimals, as the screen is not so big, see: [1.004596557012473, -0.003116992336797859, 5.936915104939593; -0.007241746117066327, 0.9973985665720294, -0.2118670500989478; 1.477734234970711e-05, -1.03363734495053e-05, 1.000089074805124] so I would like to reduce the decimals .4 or .6 or six decimals. Any ideas? Cheers If you were using printf cv::Mat data(3, 3, CV_64FC1); for (int y = 0; y < data.rows; ++y) { for (int x = 0;x < data.cols; ++x) { printf("%.6f ", data.at<double>(y, x)); } } If you were using std:

How do I separate an integer into separate digits in an array in JavaScript?

你。 提交于 2019-12-04 07:33:48
问题 This is my code so far: var n = 123456789; var d = n.toString().length; var digits = []; var squaredDigits = []; for (i = d; i >= 1; i--) { var j = k / 10; var r = (n % k / j) - 0.5; var k = Math.pow(10, i); var result = r.toFixed(); digits.push(result); } console.log(digits); But when I run my code I get this: [9, 1, 2, 3, 4, 5, 6, 7, 8] If anyone can see the problem or find a better solution I would very much appreciate it! 回答1: Why not just do this? var n = 123456789; var digits = (""+n)

recursive function digits of a positive decimal integer in reverse order c++

天涯浪子 提交于 2019-12-04 04:54:06
问题 I have an assignment to write a recursive function that writes the digits of a positive integer in reverse order. My problem is that the function doesn't display the reverse correctly. I know im supposed to use % or 10 when displaying the number and / of 10 when in the recursive call as well as the base case is supposed to be < 10. Here is my code. #include <iostream> using namespace std; int reverse(int,int); int main() { int number; int n; cout << " Enter number to reverse." << endl; cin >>

r keeping 0.0 when using paste or paste0

与世无争的帅哥 提交于 2019-12-04 03:26:58
问题 This is a simple question but it is starting to annoy me that I cant find a solution.... I would like to be able to keep the 0.0 when using it as an output when using paste or paste0 so if i have the following: y <- c(-1.5,-1.0,-0.5,0.0,0.5,1.0,1.5) > y [1] -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 paste0("x",y,"x") I get: [1] "x-1.5x" "x-1x" "x-0.5x" "x0x" "x0.5x" "x1x" "x1.5x" but want: [1] "x-1.5x" "x-1.0x" "x-0.5x" "x0.0x" "x0.5x" "x1.0x" "x1.5x" 回答1: You can use sprintf() : paste0("x", sprintf("%

Using Android:Digits attribute to restrict characters stops action next button working

若如初见. 提交于 2019-12-04 00:26:38
问题 I have a number of EditText fields on an Android app and I've used android:digits to stop invalid characters being input. eg: android:digits="@string/validchars" (where "validchars" is a string resource of all the valid characters - eg A-Z, 0-9, and some punctuation ) The problem is, if the user hits the ENTER key on the soft keyboard it used to take them to the next field but since I added android:digits bit this has stopped working. I've tried adding \n to the validchars but this just

Fastest way to find the largest power of 10 smaller than x

陌路散爱 提交于 2019-12-03 12:42:39
Is there any fast way to find the largest power of 10 smaller than a given number? I'm using this algorithm, at the moment, but something inside myself dies anytime I see it: 10**( int( math.log10(x) ) ) # python pow( 10, (int) log10(x) ) // C I could implement simple log10 and pow functions for my problems with one loop each, but still I'm wondering if there is some bit magic for decimal numbers. An alternative algorithm is: i = 1; while((i * 10) < x) i *= 10; Log and power are expensive operations. If you want fast, you probably want to look up the IEEE binary exponent in table to get the

Why `(map digitToInt) . show` is so fast?

浪尽此生 提交于 2019-12-03 12:21:49
Converting non-negative Integer to its list of digits is commonly done like this: import Data.Char digits :: Integer -> [Int] digits = (map digitToInt) . show I was trying to find a more direct way to perform the task, without involving a string conversion, but I'm unable to come up with something faster. Things I've been trying so far: The baseline: digits :: Int -> [Int] digits = (map digitToInt) . show Got this one from another question on StackOverflow: digits2 :: Int -> [Int] digits2 = map (`mod` 10) . reverse . takeWhile (> 0) . iterate (`div` 10) Trying to roll my own: digits3 :: Int ->

Delete digits in Python (Regex)

对着背影说爱祢 提交于 2019-12-03 05:06:09
问题 I am trying to delete all digits from a string. However, the next code deletes as well digits contained in any word. Obviously, I don't want that. I have been trying many regular expressions with no success. Thanks! s = "This must not be deleted, but the number at the end yes 134411" s = re.sub("\d+", "", s) print s Result: This must not b deleted, but the number at the end yes 回答1: Add a space before the \d+. >>> s = "This must not b3 delet3d, but the number at the end yes 134411" >>> s = re