I have a relatively straight through program where I collect some integer data and pass it to an external file. The problem that I have is that what I really need to do is to pass that value divided by another integer (255 to be more exact). I tried a couple of things such as:

fprintf(pFile,"%d/255 ",State[STATE_SONAR_0+i]);


fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

but that is NOT giving me the right output (the value of State[STATE_SONAR_0] divided by 255). Any idea of how I can implement it? Below is my complete program. Thank you.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Nclient.h"

void turn_front_sonar_on(void)
  int sn_order[16] = {0,2,4,6,8,10,12,14};
  conf_sn(50, sn_order);

 * main
  FILE *pFile;
  int i;
  connect_robot(1, MODEL_SCOUT2, "/dev/ttyS0", 38400);
  pFile = fopen ("log_in.txt","a");  

  /* turn on the front sonars */

  /* the loop */

    i = 0;
     fprintf(pFile,"%d ",State[STATE_SONAR_0+i]);
    } while (i < 16);

    fprintf(pFile,"%d ",State[STATE_VEL_TRANS]);
    fprintf(pFile,"_D: ");



fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255); That would be the syntactically correct way of do it.
However if the result of the division is a decimal number an int ("%d") will loose precision.
Moreover you are not checking the return of fprintf to ensure that it did the job. A negative number would indicate that there was failure.

when I tried the syntax

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

I get an output of '0'. any idea what else I could try? thanks.


when I tried the syntax

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

I get an output of '0'. any idea what else I could try? thanks.


As I said before; "%d" will loose precision if the result is a real number.
e.g. If the result is 0.1234, guess what is going to be printed to pFile?
Change "%d" to "%f".

State[STATE_SONAR] returns an integer in the range 0-255
State[STATE_VEL_TRANS] and State[STATE_VEL_STEER] both return an integer in the range -255 through 255.

what I need is the output with that number divided by 255 (I am training a neural network and it accepts inputs in the range of 0 through 1).

when I changed the "%d " to "%f " I'm getting a constant value of 2.079086 for all the variables, even though they all vary.

is there any other way that I can rewrite my program as to get the output that I'm looking for? Thanks.


you might try format precision specifiers if you want to reduce the number of decimal places. for instance, "%.2f" will round it to two decimal places, dropping any trailing zeros in the fractional part.


even better, "%8.03f" will round it to three decimal places, keep any trailing zeros in the fractional part, and keep the entire number right-justified within a constant 8-character-width field


you could also do scientific exponent format if the values are particularly small or large. For instance, "%.3e" will print (up to) three significant decimal values in exponential format, dropping any trailing zeros


and you can likewise use zero-padding and/or field width placeholders as described above.



This article has been dead for over six months. Start a new discussion instead.