compiling Problems with arduino nano

/* Autor: Thomas
 *  Studie: Funktionen
 *  Objekt: Umwandlung von Dezimal zu Binär
 */
 
void setup() {
  Serial.begin(9600);
}

void loop() {
trans(13);
}


void trans (int x) {
  for (int a=7; a>=0; a--)  {
    x = x - pow(2,a);
    if (x >= 0) {
      Serial.print ("1");
    }
      else  {
        Serial.print("0");
        x= x + pow(2,a);
      }
  } 
  delay(100000000); 
}

Hey, i have compiling problems.
I figred out, changing the code from

    if (x >= 0) {

to

    if (x >= 1) {

makes it work, but why?

 x = x - pow(2,a);

Don’t ever do this.

Try

 x -= 1<<a;

Why shouldn´t i? It actually works. My problem is the Zero, how i already mentioned.

To explain my project: I want to transform a decimal number into a binary one. That is, what i came up with.
Anybody an idea, how to get my "0" working?

Why shouldn´t i?

Try printing the values generated by "pow (2,a);"

@OP

POW() returns floating point number (in double format). How can it be related with x which is an int type variable? This is causing compilation problem.

GolamMostafa:
@OP

POW() returns float (in double format). How can it be related with x which is an int type variable? This is causing compilation problem.

It isn't causing a compilation problem, it is causing a runtime problem.

In my NANO setup, I am getting the following errors during compilation. When I bring these changes in the program (trans(13.0), void trans (float x)), the errors disappear and the sketch is uploaded.

Arduino: 1.8.5 (Windows 8.1), Board: "Arduino Nano, ATmega328P"

C:\Users\GM\AppData\Local\Temp\cckMVlLc.ltrans0.ltrans.o: In function `trans':

D:\ArduinoUNO\math-2/math-2.ino:18:(.text.startup+0x178): relocation truncated to fit: R_AVR_7_PCREL against `no symbol'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Nano.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If you get rid of the pointless (no pun intended), inaccurate floating point code, there is no problem, and the code shrinks.

AWOL:
Try printing the values generated by “pow (2,a);”

void setup() {
  Serial.begin(250000);
  for (byte count = 0; count < 8; count++) {
    Serial.print(F("pow(2, "));
    Serial.print(count);
    Serial.print(F(") = "));
    Serial.println(pow(2, count), 4);
  }
}
void loop() {}
pow(2, 0) = 1.0000
pow(2, 1) = 2.0000
pow(2, 2) = 4.0000
pow(2, 3) = 8.0000
pow(2, 4) = 16.0000
pow(2, 5) = 32.0000
pow(2, 6) = 64.0000
pow(2, 7) = 128.0000

Powers of two can be represented accurately over used range.

...and then it all goes to sh*t (or optimisation fails you) and you go back to the drawing board.
Don't use floating point methods when floating point methods are not appropriate.

AWOL:
Don't use floating point methods when floating point methods are not appropriate.

Absolutely.

But in this special case (powers of 2 with small exponents) accuracy is not the problem.
Powers of 2 have a zero mantissa, the biased exponent and are exact IIRC.

But pow(2, n) does not always return those nice exact numbers.
More decimals, please:

pow(2, 0) = 1.0000000000000000000000000
pow(2, 1) = 2.0000000000000000000000000
pow(2, 2) = 3.9999995231628417968750000
pow(2, 3) = 7.9999980926513671875000000
pow(2, 4) = 15.9999961853027343750000000
pow(2, 5) = 31.9999885559082031250000000
pow(2, 6) = 63.9999771118164062500000000
pow(2, 7) = 127.9999542236328125000000000

pow(x, y) is calculated using exp(log(x)*y)

Ok, I’m convinced. pow() does a not take the “power of two with integer exponent” accurate special route.

void setup() {
  Serial.begin(250000);
  for (byte count = 0; count < 8; count++) {
    Serial.print(F("pow(2, "));
    Serial.print(count);
    Serial.print(F(") = "));
    float result = pow(2, count);
    Serial.print(result, 5);
    Serial.print(F(", 0x"));
    pSomeHex((byte*)&result, 4);
    Serial.println();
  }
}
void loop() {}

void pSomeHex(byte* ptr, byte len) {
  while (len--) {
    pHex(*ptr++);
  }
}

void pHex(byte val) {
  pNibble(val >> 4);
  pNibble(val);
}

void pNibble(byte nib) {
  nib &= 0xF;
  Serial.write(nib + (nib < 10 ? '0' : 'A' - 10));
}
pow(2, 0) = 1.00000, 0x0000803F
pow(2, 1) = 2.00000, 0x00000040
pow(2, 2) = 4.00000, 0xFEFF7F40
pow(2, 3) = 8.00000, 0xFCFFFF40
pow(2, 4) = 16.00000, 0xFCFF7F41
pow(2, 5) = 31.99999, 0xFAFFFF41
pow(2, 6) = 63.99998, 0xFAFF7F42
pow(2, 7) = 127.99996, 0xFAFFFF42