double

GCC problem with raw double type comparisons

a 夏天 提交于 2019-12-19 01:36:12
问题 I have the following bit of code, however when compiling it with GCC 4.4 with various optimization flags I get some unexpected results when its run. #include <iostream> int main() { const unsigned int cnt = 10; double lst[cnt] = { 0.0 }; const double v[4] = { 131.313, 737.373, 979.797, 731.137 }; for(unsigned int i = 0; i < cnt; ++i) { lst[i] = v[i % 4] * i; } for(unsigned int i = 0; i < cnt; ++i) { double d = v[i % 4] * i; if(lst[i] != d) { std::cout << "error @ : " << i << std::endl; return

How do i verify a string is valid double (even if it has a point in it)?

拜拜、爱过 提交于 2019-12-18 21:06:48
问题 I have been up all night searching for a way to determine if my string value is a valid double and I haven't found a way that will also not reject a number with a point in it... In my searches I found this How to determine if a string is a number with C++? and the answer that Charles Salvia gave was bool is_number(const std::string& s) { std::string::const_iterator it = s.begin(); while (it != s.end() && std::isdigit(*it)) ++it; return !s.empty() && it == s.end(); } this works for any number

How do i verify a string is valid double (even if it has a point in it)?

戏子无情 提交于 2019-12-18 21:06:06
问题 I have been up all night searching for a way to determine if my string value is a valid double and I haven't found a way that will also not reject a number with a point in it... In my searches I found this How to determine if a string is a number with C++? and the answer that Charles Salvia gave was bool is_number(const std::string& s) { std::string::const_iterator it = s.begin(); while (it != s.end() && std::isdigit(*it)) ++it; return !s.empty() && it == s.end(); } this works for any number

Double precision value computation errors on MediaTek processors

◇◆丶佛笑我妖孽 提交于 2019-12-18 18:47:30
问题 I've found that one of my application posted on the market produces weird results on some phones. Upon investigation it turns out there is an issue with one function which computes distance between two GeoPoints - sometimes it returns completely wrong value. This issue reproduces only on devices with MediaTek MT6589 SoC (aka MTK6589). And AFAIK all of such devices have Android 4.2 installed. Update I was also able to reproduce the bug on Lenovo S6000 tablet with MediaTek MT8125/8389 chip and

how to check the data type validity of user's input (Java Scanner class)

旧街凉风 提交于 2019-12-18 17:56:19
问题 I am trying to make a simple UI that asks users to input double-type numbers, if theirs input is not of double type, the program should keep printing the prompt until user inputs a valid double type. My code below is not quite working yet, because when a user types in a valid double type, the program does not do anything unless the user types another double type number. I guess the condition (sc.hasNextDouble()) in the while loop consumes the first valid input. How to correct this? thanks a

How do I align the decimal point when displaying doubles and floats

自古美人都是妖i 提交于 2019-12-18 15:52:26
问题 If I have the following decimal point numbers: Double[] numbers = new Double[] {1.1233, 12.4231414, 0.123, 123.3444, 1.1}; for (Double number : numbers) { System.out.println(String.format("%4.3f", number)); } Then I get the following output: 1.123 12.423 0.123 123.344 1.100 What I want is: 1.123 12.423 0.123 123.344 1.100 回答1: The part that can be a bit confusing is that String.format("4.3", number) the 4 represents the length of the entire number(including the decimal), not just the part

Swift - Remove Trailing Zeros From Double

↘锁芯ラ 提交于 2019-12-18 14:15:35
问题 What is the function that removes trailing zeros from doubles? var double = 3.0 var double2 = 3.10 println(func(double)) // 3 println(func(double2)) // 3.1 回答1: In Swift 4 you can do it like that: extension Double { func removeZerosFromEnd() -> String { let formatter = NumberFormatter() let number = NSNumber(value: self) formatter.minimumFractionDigits = 0 formatter.maximumFractionDigits = 16 //maximum digits in Double after dot (maximum precision) return String(formatter.string(from: number)

How to sort a List<T> by double value?

大憨熊 提交于 2019-12-18 13:48:18
问题 This sound simple but it not that much. I want to order a List based on one of the properties of T, which is double type. 回答1: If you know the propertyname before compilation: myList = myList.OrderBy(a=>a.propertyName).ToList(); or myList = (from m in myList order by m.propertyName).ToList(); If you don't have the property at compile time (e.g. dynamic sorting in a grid or something); try the following extension methods: static class OrderByExtender { public static IOrderedEnumerable<T>

Getting unwanted NullPointerException in ternary operator - Why? [duplicate]

不想你离开。 提交于 2019-12-18 11:40:35
问题 This question already has answers here : Short form for Java If statement returns NullPointerException when one of the returned objects is null [duplicate] (3 answers) Closed last year . While executing the following code, I am getting a NullPointerException at line: value = condition ? getDouble() : 1.0; In earlier lines when I use null instead of getDouble() everything works and this is strange. public class Test { static Double getDouble() { return null; } public static void main(String[]

Could not cast value of type 'NSTaggedPointerString' to 'NSNumber'

ぃ、小莉子 提交于 2019-12-18 10:58:27
问题 I have a Swift struct like this. struct Usage { var totalData: Double var remainingTotalData: Double init(jsonData: NSData) { var jsonDict = [String: AnyObject]() do { jsonDict = try NSJSONSerialization.JSONObjectWithData(jsonData, options: []) as! [String: AnyObject] } catch { print("Error occurred parsing data: \(error)") } totalData = jsonDict["totalfup"] as! Double remainingTotalData = jsonDict["totalrem"] as! Double } } From an API, I get the following JSON response. This is the println