double

Sorting a double value of an object within an arrayList

拟墨画扇 提交于 2020-01-10 08:09:28
问题 I'm trying to sort my custom class chromosome by the value of their score attribute which is a double. These chromosomes are stored within an ArrayList. I know I have to use a comparator but I've read so many differing opinions online in the last hour that I'm utterly confused. Attached is my code, if someone could point me in the right direction I would be much appreciated. public class Chromosome { public Gene[] genes; public double score; public Chromosome(int l) { genes = new Gene[l]; }

Fahrenheit to Celsius conversion

旧巷老猫 提交于 2020-01-10 06:07:42
问题 I'm trying to write a program that converts temperatures expressed in degree Fahrenheit to degree Celsius. The user enters a Fahrenheit value and the program prints out the Celsius value. The user is supposed to enter 212 as the Fahrenheit value the program is supposed to calculate 100.0 as the Celsius value but instead I'm getting 0.0 as the Celsius value when I want to get 100.0 I'm not sure where the problem might be. Is it possibility the order of operations? The formula for converting

Convert double to byte[] array

巧了我就是萌 提交于 2020-01-10 05:54:05
问题 How can I convert double to byte array in Java? I looked at many other posts, but couldn't figure out the right way. Input = 65.43 byte[] size = 6 precision = 2 (this might change based on input) expected output (byte[]) = 006543 Can I do it without using functions like doubleToLongBits()? 回答1: Real double to byte[] Conversion double d = 65.43; byte[] output = new byte[8]; long lng = Double.doubleToLongBits(d); for(int i = 0; i < 8; i++) output[i] = (byte)((lng >> ((7 - i) * 8)) & 0xff); /

How to check if float can be exactly represented as an integer

若如初见. 提交于 2020-01-10 04:50:07
问题 I'm looking to for a reasonably efficient way of determining if a floating point value ( double ) can be exactly represented by an integer data type ( long , 64 bit). My initial thought was to check the exponent to see if it was 0 (or more precisely 127 ). But that won't work because 2.0 would be e=1 m=1... So basically, I am stuck. I have a feeling that I can do this with bit masks, but I'm just not getting my head around how to do that at this point. So how can I check to see if a double is

Java Double vs double: class type vs primitive type

只愿长相守 提交于 2020-01-09 13:52:08
问题 I was curious to what the performance differences between Java's class and primitive type for double were. So I created a little benchmark and found the class type to be 3x-7x slower than the primitive type. (3x on local machine OSX, 7x on ideone) Here is the test: class Main { public static void main(String args[]) { long bigDTime, littleDTime; { long start = System.nanoTime(); Double d = 0.0; for (Double i = 0.0; i < 1432143.341; i += 0.1) { d += i; } long end = System.nanoTime(); bigDTime

Can C# store more precise data than doubles?

六眼飞鱼酱① 提交于 2020-01-09 11:35:51
问题 double in C# don't hold enough precision for my needs. I am writing a fractal program, and after zooming in a few times I run out of precision. I there a data type that can hold more precise floating-point information (i.e more decimal places) than a double? 回答1: Yes, decimal is designed for just that. However, do be aware that the range of the decimal type is smaller than a double. That is double can hold a larger value, but it does so by losing precision. Or, as stated on MSDN: The decimal

Convert strings to int or float in Python 3?

こ雲淡風輕ζ 提交于 2020-01-09 11:34:51
问题 integer = input("Number: ") rslt = int(integer)+2 print('2 + ' + integer + ' = ' + rslt) double = input("Point Number: ") print('2.5 + ' +double+' = ' +(float(double)+2.5)) Gives me Traceback (most recent call last): File "C:\...", line 13, in <module> print('2 + ' + integer + ' = ' + rslt) TypeError: Can't convert 'int' object to str implicitly I'm fairly new to programming and my background is mostly just the basics of C# so far. I wanted to try to learn Python through doing all my C#

Convert strings to int or float in Python 3?

时光总嘲笑我的痴心妄想 提交于 2020-01-09 11:33:02
问题 integer = input("Number: ") rslt = int(integer)+2 print('2 + ' + integer + ' = ' + rslt) double = input("Point Number: ") print('2.5 + ' +double+' = ' +(float(double)+2.5)) Gives me Traceback (most recent call last): File "C:\...", line 13, in <module> print('2 + ' + integer + ' = ' + rslt) TypeError: Can't convert 'int' object to str implicitly I'm fairly new to programming and my background is mostly just the basics of C# so far. I wanted to try to learn Python through doing all my C#

Why does median trip up data.table (integer versus double)?

最后都变了- 提交于 2020-01-09 09:10:08
问题 I have a data.table called enc.per.day for encounters per day. It has 2403 rows in which a date of service is specified and the number of patients seen on that day. I wanted to see the median number of patients seen on any type of weekday. enc.per.day[,list(patient.encounters=median(n)),by=list(weekdays(DOS))] That line gives an error Error in [.data.table (enc.per.day, , list(patient.encounters = median(n)), : columns of j don't evaluate to consistent types for each group: result for group 4

Double precision problems on .NET

烈酒焚心 提交于 2020-01-08 16:35:09
问题 I have a simple C# function: public static double Floor(double value, double step) { return Math.Floor(value / step) * step; } That calculates the higher number, lower than or equal to "value", that is multiple of "step". But it lacks precision, as seen in the following tests: [TestMethod()] public void FloorTest() { int decimals = 6; double value = 5F; double step = 2F; double expected = 4F; double actual = Class.Floor(value, step); Assert.AreEqual(expected, actual); value = -11.5F; step = 1