Using LOG in VISUALG Functions

2

When using LOG in visualG a problem is occurring, I need to split a value entered by the user, by log 2 in base 10. But he does not do this error.

funcao menu_8():inteiro
var
logaritmo :real
inicio
para contador de 0 ate 12 faca
      logaritmo <- ((log(i[contador])) / (log(2)))
      se ((logaritmo % 2 = 0) OU (logaritmo % 2 = 1)) entao
         escreva(" é Potencia de 2: ", i[contador])
      fimse
fimpara
escreval(" ")
fimfuncao

After the SE function is executed and the result of the division is an integer value, without a comma, it prints the message, otherwise it makes the loop of repetition with another value of the vector, until it finds a number that is power of 2, if none, then it goes out and does not print anything.

What is the easiest way to resolve this?

    
asked by anonymous 17.06.2016 / 15:43

1 answer

1

The problem is in the comparison, exactly in the SE, you should check if the result is integer otherwise if you do not have a function relative to "module" ( MOD ) that usually captures only the a fractional part of a Real number, not all languages have, if not, do with the function that takes the whole part, like this:

If INT (Result) - Result = 0 , then the operation has no remainder, ie it is an integer.

For example:

Se   INT(123) - 123 = 0  'Então  123  É um número inteiro

Se   INT(45,678) - 45,678 = 0,678  'Então  45,678 NÃO É um número inteiro
    
03.07.2016 / 21:40