Problems with addition of float variables

This is a discussion on Problems with addition of float variables within the C Programming forums, part of the General Programming Boards category; When incrementing the float variable cf using one of the two statements cf=cf+5; or cf=cf+inc; where inc is a float ...

  1. #1
    Phanixis
    Guest

    Angry Problems with addition of float variables

    When incrementing the float variable cf using one of the two statements

    cf=cf+5;

    or

    cf=cf+inc;

    where inc is a float variable that was set equal to 5

    the value of cf is increased, but it is increased by a value other than 5

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I've never heard or seen of this but try adding a decimal point to the end and/or 'f'

    Code:
    float var = 5.0f;
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Phanixis
    Guest
    Ok, here is some of the code:


    int y;
    float cf, inc, data;
    char temp, ccf; //used as array

    ....

    cf=300;
    inc=5;

    for (y=0; y<=3; y++)
    {
    printf("%f", cf); //to test the code
    sprintf(ccf, "CF %fMZ", cf);

    ibwrt(sa, ccf, 20L); //writes assembly to a GPIB bus
    //sa is the bus address
    sleep(1);

    ibwrt(sa, "E1", 2L);
    ibwrt(sa, "MA", 2L);
    ibrd(sa, temp, 8L);

    sscanf(temp, "%f", &data);

    fprintf(dataout, "%f\n", data); //dataout defined elsewhere

    cf = cf + inc;
    }

    when running this segment of code, the printf statement at the beginning of the loop will print:

    300
    11,234,345 or similiar
    additional bizarre number
    additional bizarre number

    I hope that helps.

    Thanks.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    i'm not really sure what's going on here.

    But just to point out something (don't know if it'll have an effect or not...)

    you initialized ccf as char
    but using sprintf() you treated it like a string...

    try
    Code:
    char ccf[8];
    or if thats not enough
    Code:
    char ccf[16];
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    float cf, inc, data;
    char temp, ccf; //used as array
    
    ....
    
    cf=300;
    inc=5;
    
    Why floats? cf and inc are not floats in this case, they are integers. Even though most smart compilers will convert them to floats 300.0f and 5.0f, you should do this yourself.

    I don't understand why you are using - make that misusing -floats.

    Again and again I've looked at this code and it looks like some attempt to write a crude compiler for something. Are you trying to place assembly opcodes directly into memory or something?? I'm totally lost.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by Bubba
    Why floats? cf and inc are not floats in this case, they are integers. Even though most smart compilers will convert them to floats 300.0f and 5.0f, you should do this yourself.

    I don't understand why you are using - make that misusing -floats.

    Again and again I've looked at this code and it looks like some attempt to write a crude compiler for something. Are you trying to place assembly opcodes directly into memory or something?? I'm totally lost.
    Here's a hint: cf stands for center frequency. Want to take a guess what sa stands for?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  2. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:17 AM
  3. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  4. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 11:56 AM
  5. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21