I try to use the codes below to get the abs value of a long long type integer;
long long v=abs(originalValue);
It works as I expected until the value of v exceeds 1073741824 (1G)
If v is 2147482648, abs(v) is -2147482648.
If v is 10737418240, abs(v) is -2147482648 also.
I do not understand what causes these happened.
Welcome any comment
Thanks
interdev
Use llabs() instead:
long long v = llabs(originalValue);
abs takes an int as an argument, for a long long use llabs
Related
I have ported Java code to C#.
Could you please explain why I have compile-time error in the follow line (I use VS 2008):
private long l = 0xffffffffffffffffL; // 16 'f' got here
Cannot convert source type ulong to target type long
I need the same value here as for origin Java code.
Java doesn't mind if a constant overflows in this particular situation - the value you've given is actually -1.
The simplest way of achieving the same effect in C# is:
private long l = -1;
If you want to retain the 16 fs you could use:
private long l = unchecked((long) 0xffffffffffffffffUL);
If you actually want the maximum value for a signed long, you should use:
// Java
private long l = Long.MAX_VALUE;
// C#
private long l = long.MaxValue;
Assuming you aren't worried about negative values, you could try using an unsigned long:
private ulong l = 0xffffffffffffffffL;
In Java the actual value of l would be -1, because it would overflow the 2^63 - 1 maximum value, so you could just replace your constant with -1.
0xffffffffffffffff is larger than a signed long can represent.
You can insert a cast:
private long l = unchecked( (long)0xffffffffffffffffL);
Since C# uses two's complement, 0xffffffffffffffff represents -1:
private long l = -1;
Or declare the variable as unsigned, which is probably the cleanest choice if you want to represent bit patterns:
private ulong l = 0xffffffffffffffffL;
private ulong l = ulong.MaxValue;
The maximal value of a singed long is:
private long l = 0x7fffffffffffffffL;
But that's better written as long.MaxValue.
You could do this:
private long l = long.MaxValue;
... but as mdm pointed out, you probably actually want a ulong.
private ulong l = ulong.MaxValue;
object LPrimeFactor {
def main(arg:Array[String]):Unit = {
start(13195)
start(600851475143)
}
def start(until:Long){
var all_prime_fac:Array[Int] = Array()
var i = 2
(compile:compileIncremental) Compilation failed
integer number too large
Even though I changed the arg type to Long, it's still not fixed.
Pass the argument as a Long (notice the L at the end of the number):
start(600851475143L)
// ^
To create a Long literal you must add L to the end of it.
start(600851475143L)
Please remember literals values, if you has not any type direct suffix, the compiler try to get your numeric type values, such as 600851475143 as type Int, which is 32-bit length, two complement representation
MIN_VALUE = -2147483648(- 2 ^ 31)
MAX_VALUE = 2147483647(2 ^ 31 - 1)
So please add right suffix on the literal value, as 600851475143L
It is defined that UInt is the type of unsigned integer. But in such case it seems like the MSB is still a sign. e.g., the most relative QA is Chisel UInt negative value error which works out a workaround but no why. Could you enlight me about the 'why'?
The UInt seems to be defined in chisel3/chiselFrontend/src/main/scala/chisel3/core/Bits.scala but I cannot understand the details. Is the UInt is derived from Bits and Bits is derived from Int of scala?
The simple answer is that this is due to how Scala evaluates things.
Consider an example like
val x = 0xFFFFFFFF.U
This statement causes an error.
UInt literal are represented internally by BigInts, but the 0xFFFFFFFF is an specifying an Int value. 0xFFFFFFFF is equivalent to the Int value -1.
The -1 Int value is converted to BigInt -1 and -1.U is illegal because the .U literal creation method will not accept negative values.
Adding the L fixes this because 0xFFFFFFFL is a positive Long value.
The issue is that Scala only has signed integers, it does not have an unsigned integer type. From the REPL
scala> 0x9456789a
res1: Int = -1806272358
Thus, Chisel only sees the negative number. UInts obviously cannot be negative so Chisel reports an error.
You can always cast from an SInt to a UInt if you want the raw 2's complement representation of a negative number interpreted as a UInt. eg.
val a = -1.S(32.W).asUInt
assert(a === "xffffffff".U)
I have just started using selenium IDE and I am trying to append a integer value after a constant string:
variable x = string
variable y = integer
append string+integer
anyone so kind to help me out?
I have never used java in my life.
Thank you!
You can store the values in another string like:
String name = "test";
int number = 1;
String combined = name+number;
Hej folks, I'm quite the beginner in programming but I read my share of stackoverflow pages, and googled a bit as well, still can't figure if the following is even possible in FORTRAN 90.
I'm trying to isolate the digits in an integer, to point where the hurdle is, consider the following idea :
INTEGER :: n, mult, add
READ *, n ! n = 8
mult = n*2 ! = 16
add = ??? ! where I want to add 1 + 6
Another way, I trust that this will be obvious to anyone reading the code:
INTEGER FUNCTION sum_digits(num)
INTEGER, INTENT(in) :: num
INTEGER, DIMENSION(:), ALLOCATABLE :: digs
INTEGER :: num_digits, ix, rem
num_digits = FLOOR(LOG10(REAL(num))+1)
ALLOCATE(digs(num_digits))
rem = num
DO ix = 1, num_digits
digs(ix) = rem - (rem/10)*10 ! Take advantage of integer division
rem = rem/10
END DO
sum_digits = SUM(digs)
END FUNCTION sum_digits
I've subjected this to a quick series of obvious tests and it has passed all 4 of them. If you find a case for which it doesn't work, fix it. And if you want the array of digits returned, modify the function to return that. If you want it to work for negative integers too throw in ABS() at an appropriate place.
one way to pull off the 'ith' place digit is:
n/10**i-10*(n/10**(i+1))
so for your example:
n-10*(n/10) + n/10-10*(n/100)