numbers

Irrational number representation in any programming language?

瘦欲@ 提交于 2019-12-30 06:13:58
问题 Does anyone know of an irrational number representation type/object/class/whatever in any programming language? All suggestions welcome. Simply put, if I have two irrational objects, both representing the square root of five, and I multiply those objects, I want to get back the integer five, not float 4 point lots o' 9s. Specifically, I need the representation to be able to collect terms, not just resolve every time to an integer/float. For instance, if I want to add the square root of five

Swift 2.0 Format 1000's into a friendly K's

血红的双手。 提交于 2019-12-30 05:59:26
问题 I'm trying to write a function to present thousands and millions into K's and M's For instance: 1000 = 1k 1100 = 1.1k 15000 = 15k 115000 = 115k 1000000 = 1m Here is where I got so far: func formatPoints(num: Int) -> String { let newNum = String(num / 1000) var newNumString = "\(num)" if num > 1000 && num < 1000000 { newNumString = "\(newNum)k" } else if num > 1000000 { newNumString = "\(newNum)m" } return newNumString } formatPoints(51100) // THIS RETURNS 51K instead of 51.1K How do I get

Swift 2.0 Format 1000's into a friendly K's

断了今生、忘了曾经 提交于 2019-12-30 05:58:02
问题 I'm trying to write a function to present thousands and millions into K's and M's For instance: 1000 = 1k 1100 = 1.1k 15000 = 15k 115000 = 115k 1000000 = 1m Here is where I got so far: func formatPoints(num: Int) -> String { let newNum = String(num / 1000) var newNumString = "\(num)" if num > 1000 && num < 1000000 { newNumString = "\(newNum)k" } else if num > 1000000 { newNumString = "\(newNum)m" } return newNumString } formatPoints(51100) // THIS RETURNS 51K instead of 51.1K How do I get

Why is '…' concatenating two numbers in my code?

自作多情 提交于 2019-12-30 05:55:08
问题 I have the following code snippet where I don't really understand its output: echo 20...7; Why does this code output 200.7 ? From what I know ... is the splat operator, which it is called in ruby, that lets you have a function with a variable number of arguments, but I don't understand what it does here in the context with echo . Can anyone explain what exactly this code does? 回答1: No this is not the splat/unpacking operator, even thought it might seem like it is. This is just the result of

printf displays different values for the same variable

穿精又带淫゛_ 提交于 2019-12-30 05:54:12
问题 I found this code online and when compiled it prints 5 and 10: int numbеr = 5; int number = 10; System.out.printf("Number one is %d and number two is %d.", numbеr, number); How can this work ?! 回答1: Java supports Unicode characters, one or more of the letters in one of the "numbers" variable is unicode letter from different alphabet, you can check this by copy-pasting both of those names and trying to do this: System.out.println("numbеr".equals("number")); They seem same to the naked eye, but

Most elegant way to detect if a String is a number?

南笙酒味 提交于 2019-12-30 02:25:49
问题 Is there a better, more elegant (and/or possibly faster) way than boolean isNumber = false; try{ Double.valueOf(myNumber); isNumber = true; } catch (NumberFormatException e) { } ...? Edit : Since I can't pick two answers I'm going with the regex one because a) it's elegant and b) saying "Jon Skeet solved the problem" is a tautology because Jon Skeet himself is the solution to all problems. 回答1: I don't believe there's anything built into Java to do it faster and still reliably, assuming that

C++ complex numbers, what is the right format?

时光怂恿深爱的人放手 提交于 2019-12-30 00:07:25
问题 I want to use C++ with complex numbers. Therefore I included #include <complex> . Now my question is: How do I declare a variable?(so what is the format called for let's say: 1 + i ?) Thanks in advance :-) 回答1: // 1 + 2i std::complex<double> c(1, 2); 回答2: The constructor of std::complex has two parameters: The first, wich has the real part of the number. The second, wich has the imaginary part of the number. For example: std::complex<float> my_complex(1,1); //1 + 1i Also, C++11 introduces

Only specific numbers in edit box

女生的网名这么多〃 提交于 2019-12-29 09:58:10
问题 I would like that user could write only numbers from 1 to 49 in edit box. I know how to exclude letters and has possibility to put only numbers but I can't limit it to specific numbers (e.g from 1 to 49 - like in lottery game). I added KeyDown event to edit box and put this code: if not (KeyChar in ['1'..'9']) then begin ShowMessage('Invalid character'); KeyChar := #0; end; How can I modify it? 回答1: Following David's advice, a pattern I often use looks something like this : function

regex compare two numbers

﹥>﹥吖頭↗ 提交于 2019-12-29 09:32:47
问题 can i somehow compare two numbers in regex? i want regex that is correct for 10-12, but incorrect for 12-10. I mean that 10 must be smaller than 12. I want to do it in Javascript. 回答1: If the input is always of the form X-Y, then why not use the split() function with '-' as the delimiter and then compare the two parts with > You can't compare numerical values using RegExps. 回答2: I wouldn't use regex for this. I'd split the string on the operator, then compare the two resulting numbers based

RowFilter.NumberFilter: can't handle “mixed” concrete number types

社会主义新天地 提交于 2019-12-29 08:23:37
问题 Happens if at least one of the values (values == value in RowFilter, value in entry) is a decimal. Here's a failing test: @Test public void testRowFilterNumberMixCore() { TestEntry entry = new TestEntry(1.2f); RowFilter filter = RowFilter.numberFilter(ComparisonType.AFTER, 1, 0); assertTrue(entry + "must be included " + filter, filter.include(entry)); } The output is: junit.framework.AssertionFailedError: [entry: 1.2] must be included [RowFilter: ComparisonType = AFTER, comparableValue: 1,