small problem with pow

Hello to all !. I made a program that reads the numbers contained in an int vector and transforms it to a number. When i run it, for some reason i don’t understand, it subtracts one to the real value. I made several cold runs and the logic seems right, i also used a c++ compiler and it works like a charm.

I have an arduino yún with the 1.5.8 IDE.

This is the code:

#include<math.h>
void setup() {
delay(10000);
unsigned int j=0,i=0;
unsigned int test=0;
unsigned int buff2[4]={5,9,0,0};
i=4;
while (j < i)
{
test=test+(buff2[j]*(pow(10,(i-j-1))));
j++;
}
Serial.println(test);
}

void loop() {
// put your main code here, to run repeatedly:
}
The output of this program is 5899.

I don’t know is there is something wrong with the program (maybe with the pow expression), or there is a bug in the IDE and i don’t want to add one to test before understanding the real problem.

What really confuses me is that in c++ it gives the real value.

Thanks in advance :).

Why use pow? What's wrong with simple multiplication?

Ok i just realized that this is a solved in http://forum.arduino.cc/index.php?topic=40776.0. Apparently there is an error due to conversion from float (that's what pow receives) to int. Still i don't understand why c++ gets it right.

Sorry for the inconvenience.

pow may well work better on 64 bit "double"s, but not so well on the 8bit Arduino’s 32 bit "double"s
The language is still C++.

I use pow because i wan to to use it for multiple items. This is a example script to demonstrate the problem. I really use this to take values of a text that is in a file on a sd card. I want to transform the value that is in the file to a int array so i can use it on another function.

This is the code:

#include <Bridge.h>
#include <FileIO.h>
void setup() {
Bridge.begin();
delay(10000);
if(FileSystem.begin()) {
Serial.println(“Memory OK !!!”);
}
if (FileSystem.exists("/mnt/sd/test.txt")) { //test.txt exists??
Serial.println(“test.txt exists”);
}
File archivo=FileSystem.open("/mnt/sd/test.txt", FILE_READ);
unsigned int test[19]={0};
char buff;
int buff2[3];
unsigned int i=0, k=0, j=0;
if (archivo)
{
while (archivo.available()) {
buff=archivo.read();
if ((buff != 32) && (buff != 10))
{
buff2*=buff - 48;*

  • i++;*

  • } //fin if buff != 32*

  • else*

  • {*

  • j=0;*

  • while (j < i)*

  • {*
    _ test[k]=test[k]+(buff2[j]*(pow(10,(i-j-1))));_

  • j++;*

  • }*

  • test[k]+=1; //I don’t have to use this in c++ :(*

  • k++;*

  • i=0;*

  • } //fin else buff == 32 *

  • } *

  • archivo.close();*

  • for (i=0;i<19;i++)*

  • {*
    _ Serial.print(test*);_
    _
    Serial.print(", ");_
    _
    }_
    _
    }_
    else
    _
    { Serial.println(“Error opening archivo”);}*_

*} *
void loop() {
* // put your main code here, to run repeatedly:*
}
This runs ok but i have to make test*+=1 so it gives the real values.*

Oh, I forgot - remember to use code tags when posting code.

Wow AWOL i didn't think about it that way, you are right we are using a 8 bit Arduino and the double are not the same. I used pow just as a shortcut.

Thanks a lot :).

P.D. Sorry about the code tags i will use them in my next posts :sweat_smile: