double

Ranges in Kotlin using data type Double

北慕城南 提交于 2019-12-12 10:36:16
问题 fun calcInterest(amount: Double, interest: Double): Double { return(amount *(interest/100.0)) } fun main(args: Array<String>) { for (i in 1.0..2.0 step .5) { println("&10,000 at 5% interest is = ${calcInterest(10000.0,i)}") } } I get the error saying the For-loop range must have an 'Iterator()'Method. It underlines my doubles in the section (i in 1.0..2.0) How can I use doubles in a range?? A website on Ranges Reloaded (https://blog.jetbrains.com/kotlin/2013/02/ranges-reloaded/ ) shows that

How does Visual Studio display a System.Double during debugging?

雨燕双飞 提交于 2019-12-12 08:45:10
问题 Try debugging the following simple program, and mouse over x in each step (or "Add Watch" for x or whatever). using System; using System.Globalization; static class Program { static double x; static void Main() { x = 2d; // now debugger shows "2.0", as if it has used // x.ToString("F1", CultureInfo.InvariantCulture) x = 8.0 / 7.0; // now debugger shows "1.1428571428571428", as if it had used // x.ToString("R", CultureInfo.InvariantCulture) // Note that 17 significant figures are shown, not

What's a good way to check if a double is an integer in C#? [duplicate]

旧城冷巷雨未停 提交于 2019-12-12 08:19:06
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: How to determine if a decimal/double is an integer? I have a variable of type double and am wanting to check whether it is an integer. At the moment I have public bool CheckIfInteger(double number) { return number.ToString().Contains(".") == false; } Is there a better way? UPDATE: Sorry I didn't realise the potential for confusion, by integer I meant the mathematical definiton of integer, that is the natural

What is the best way to separate double into two parts “integer & fraction” in java

偶尔善良 提交于 2019-12-12 07:46:32
问题 I have tried to separate 5.6 (for example) by the following method: private static double[] method(double d) { int integerPart = 0; double fractionPart = 0.0; integerPart = (int) d; fractionPart = d - integerPart; return new double[]{integerPart, fractionPart}; } But what I got is: [0] = 5.0 [1] = 0.5999999999999996 Do you have any suggestion about doing this without converting the number to string? 回答1: Use BigDecimal to do that same calculation. (using doubles has precision problems because

Why double.MaxValue is larger than long.MaxValue?

谁都会走 提交于 2019-12-12 07:45:19
问题 both of them hold 8 bytes, but how come the max value for double is much greater than the max value of long? there is a finite number of bits available, so how could you reach greater numbers with floating point variables? 回答1: It uses a different representation (floating point) using exponents and mantissa For details see IEEE754 回答2: A double has something called an exponent, which is basically just a scaling factor. This allows the range of double to be much greater, but at the cost of

C# Type suffix for decimal

雨燕双飞 提交于 2019-12-12 07:44:11
问题 I don't know what the correct wording is for what I am trying to achieve so it may already be posted online. Please be kind if it is. Ok so basically I have this method. public static T IsNull<T>(IDataReader dr, String name, T nullValue) { return Helpers.IsNull(dr, dr.GetOrdinal(name), nullValue); } public static T IsNull<T>(IDataReader dr, Int32 index, T nullValue) { if (dr.IsDBNull(index)) { return nullValue; } else { return (T)dr.GetValue(index); } } Being called as Helpers.IsNull(dr,

Double to fraction in Java

孤者浪人 提交于 2019-12-12 07:18:39
问题 So what I'm trying to do is convert double to rational number. I check how many digits there is after decimal point and I want to save the number 123.456 as 123456 / 1000, for example. public Rational(double d){ String s = String.valueOf(d); int digitsDec = s.length() - 1 - s.indexOf('.'); for(int i = 0; i < digitsDec; i++){ d *= 10; } System.out.println((int)d); //checking purposes } However, for the number 123.456 I get a round off error and the result is 123455. I guess it'd be possible to

Double from long bits

不打扰是莪最后的温柔 提交于 2019-12-12 07:16:03
问题 I have an unsigned long long (or uint64_t ) value and want to convert it to a double . The double shall have the same bit pattern as the long value. This way I can set the bits of the double "by hand". unsigned long long bits = 1ULL; double result = /* some magic here */ bits; I am looking for a way to do this. 回答1: The portable way to do this is with memcpy (you may also be able to conditionally do it with reinterpret_cast or a union, but those aren't certain to be portable because they

Exception for 32 bit number C++

那年仲夏 提交于 2019-12-12 06:54:24
问题 Why do I get exception Unhandled exception at 0x00000001 in TestingCOA.exe: 0xC0000005: Access violation (parameters: 0x00000008). when I try to work with a 4294967295 or higher number. On my machine sizeof double is 8bytes which should be able to handle and work with 2^64 -1 number but it is generating exception for a 32 bit number, why is that? int main() { double n,remainderA; int AfterDecimal1[64],RemExponent1; cout<< "Enter number\n"; cin>> n; remainderA=a-(int)a; HandleFractionNumber

Unable to read double or int after first adding second string

北城余情 提交于 2019-12-12 06:49:29
问题 so I'm very very new to java and I'm having a slight problem. I'm adding various doubles and integers and trying to read data from a file, but when I try read data from the file after the second string, I am given an error. Anything before the second string is fine. Here's my code; import java.util.*; import java.io.*; public class Assignment3 { static Scanner console = new Scanner (System.in); public static void main(String[] args) throws FileNotFoundException { Scanner inFile = new Scanner