long-integer

Java: many ways of casting a (long) Object to double

核能气质少年 提交于 2020-01-23 04:55:08
问题 I have an Object obj that I know is actually a long . In some Math code I need it as double . Is it safe to directly cast it to double? double x = (double)obj; Or should I rather cast it first to long and then to double. double x = (double)(long)obj; I also found another (less readable) alternative: double x = new Long((long)obj).doubleValue(); What are the dangers/implications of doing either? Solution Summary : obj is a Number and not a long . Java 6 requires explicit casting, e.g.: double

I want to know the difference between a long double and a double

岁酱吖の 提交于 2020-01-17 18:24:07
问题 For any algorithmic problem, an error of 10 -6 is allowed. I declared a long double as a variable in my first solution and got a WA. But, when I declared a variable as double , I got an AC. I want to know why this decision was made because long double is known to be more accurate than double . I have not changed anything except variables, output methods. Here is my code: #include <iostream> #include <string> #include <cmath> #include <vector> #include <queue> #include <deque> #include

wide to long multiple columns issue

佐手、 提交于 2020-01-16 09:48:16
问题 I have something like this: id role1 Approved by Role1 role2 Approved by Role2 1 Amy 1/1/2019 David 4/4/2019 2 Bob 2/2/2019 Sara 5/5/2019 3 Adam 3/3/2019 Rachel 6/6/2019 I want something like this: id Name Role Approved 1 Amy role1 1/1/2019 2 Bob role1 2/2/2019 3 Adam role1 3/3/2019 1 David role2 4/4/2019 2 Sara role2 5/5/2019 3 Rachel role2 6/6/2019 I thought something like this would work melt(df,id.vars= id, measure.vars= list(c("role1", "role2"),c("Approved by Role1", "Approved by Role2")

Performance loop with integer vs Long index

时光总嘲笑我的痴心妄想 提交于 2020-01-16 04:13:47
问题 I am wondering why it takes so much longer to run a loop with a long index vs. an integer index? Any idea? Thanks int n = 1000_000_000; long n2 =n; long t1 = System.currentTimeMillis(); for( int idx = 0; idx<n;idx++){ } long t2 = System.currentTimeMillis(); for( long idx = 0; idx<n2;idx++){ } long t3 = System.currentTimeMillis(); long dt1 = t2-t1; long dt2 = t3-t2; System.out.println("with int = took " + dt1 +"ms"); System.out.println("with long = took " + dt2 +"ms"); 回答1: It possible has

Java 6 - Creating and detecting the first double value above Long.MAX_VALUE

六眼飞鱼酱① 提交于 2020-01-15 03:28:07
问题 Below I attempt to assign to value the maximum Long value, then add to it the minimum positive Double value possible. I then try to detect that the value is greater than the maximum Long value. Double value = new Long(Long.MAX_VALUE).doubleValue(); value += Double.MIN_VALUE; if (value < -(new Long(Long.MAX_VALUE).doubleValue()) || value > new Long(Long.MAX_VALUE).doubleValue()) { // Expecting code here to execute, but it doesn't. } Studying the values involved shows that value has the final

Python sum() returns negative value because the sum is too large for 32bit integer

人走茶凉 提交于 2020-01-14 14:47:31
问题 x = [1, 2, 3, ... ] y = sum(x) The sum of x is 2165496761, which is larger than the limit of 32bit integer So sum(x) returns -2129470535. How can I get the correct value by converting it to long integer? Here is my import list: import math, csv, sys, re, time, datetime, pickle, os, gzip from numpy import * 回答1: The reason why you get this invalid value is that you're using np.sum on a int32 . Nothing prevents you from not using a np.int32 but a np.int64 or np.int128 dtype to represent your

Trouble with printf conversion long long

心已入冬 提交于 2020-01-14 14:20:12
问题 I've been working on a project euler problem, which by their very nature coerce you to use data types with big storage. #include <stdio.h> #include <conio.h> #define num 600851475143 int main() { long long i, j, count=0, number=num, k; for(i=2;number!=1;i++) { count=0; for(j=1;j<=i;j++) { if((i%j)==0) { count++; } } for(k=0;k<100000000;k++) {} if(count==2) { printf(" %d\n", i); if(number%i==0) { number/=i; printf(" %d\n", number); printf("%d\n", i); i=2; } } } getch(); return 0; } When I

Why not use long for all integer values

北城余情 提交于 2020-01-12 06:56:26
问题 In my Java class we have just learned about of each of the following primitive data types: byte short int long Since the long data type contains the most bits, wouldn't it make sense to exclusively use the long data type to avoid restrictions? Questions Is there a particular downside to only using the long data type? Does it make sense to use for example, an int data type, instead of a long data type? 回答1: Does it make sense to use for example, an int data type, instead of a long data type?

Why negative value comes for long after multiplication? [duplicate]

天大地大妈咪最大 提交于 2020-01-11 14:26:33
问题 This question already has answers here : Why do these two multiplication operations give different results? (2 answers) Why can't I assign a 'long' a value of 4 billion? (6 answers) Closed 5 years ago . Why this code in java gives negative value? long ttt = (60 * 60 * 1000 * 24 * 26); System.out.println(ttt); Result which comes as on eclipse console -2134967296 ? Anything silly I am doing, may be it crossed int range I guess? 回答1: Because 60 * 60 * 1000 * 24 * 25 overflows in the int range.

Java: Why does “long” number get negative?

梦想与她 提交于 2020-01-11 09:46:24
问题 I have this code: long i = 0; while (true) { i += 10*i + 5; System.out.println(i); Thread.sleep(100); } Why does the long i get negative after a few prints? If the range is exceeded, shouldn't an error occur? 回答1: Java doesn't throw an error if you increase a number after its maximum value. If you wish to have this behaviour, you could use the Math.addExact(long x, long y) method from Java 8. This method will throw an ArithmeticException if you pass the Long.MAX_VALUE . The reason why Java