+ Start a Discussion
SFDC-SDSFDC-SD 

Exponential Operator Issue - I can't believe it

Guys...

 

Has any body faced this issue?

 

integer exponent2 = 2^8;
system.debug('Exponent ' +exponent2);

 

Output is:

10 (Actual value should be 256)

 

 

As per Salesforce Manual:

Math Operator ^  Raises a number to a power of a specified number.

 

Best Answer chosen by Admin (Salesforce Developers) 
Rajesh_ShahRajesh_Shah

The definition of ^ is not of exponent in the apex guide. It is mentioned as 

"Bitwise exclusive OR operator. Exclusive ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if exactly one of the bits is set to 1 and the other bit is set to"

 

For doing exponent, you can use the pow function. 

Eg:

Decimal myDecimal = 4; 
Decimal powDec = myDecimal.pow(2); 
system.assertEquals(powDec, 16);

 

All Answers

Rajesh_ShahRajesh_Shah

The definition of ^ is not of exponent in the apex guide. It is mentioned as 

"Bitwise exclusive OR operator. Exclusive ORs each bit in x with the corresponding bit in y so that the result bit is set to 1 if exactly one of the bits is set to 1 and the other bit is set to"

 

For doing exponent, you can use the pow function. 

Eg:

Decimal myDecimal = 4; 
Decimal powDec = myDecimal.pow(2); 
system.assertEquals(powDec, 16);

 

This was selected as the best answer
SFDC-SDSFDC-SD

 

I am not sure which document you are referring to... 

 

http://login.salesforce.com/help/doc/en/customize_functions.htm#PowerDef

 

^ (Exponentiation)
Description: 	Raises a number to a power of a specified number.
Use: 	number^integer and replace number with a merge field, expression, or another numeric value; replace integer with a merge field that contains an integer, expression, or any integer.

Example: 
NumberOfEmployees^4 calculates the number of employees to the 4th power.

Report Example: 

ACTIVE:SUM ^ 2 calculates the number of active Salesforce users to the 2nd power for administration. This formula is a number data type that returns a positive integer.

Tips: 	Avoid replacing integer with a negative number.

 

Rajesh_ShahRajesh_Shah

I was referring to the Apex Developers Guide. The link you have given is for operators when used in Formula; not apex. (I believe you are writing Apex code.)

SFDC-SDSFDC-SD

My mistake... never used this method or operator and was blindly searching for answers all over.

Thanks for the alert and the Answer.