Thread: display some garbage value while subtraction in c prorgaming

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    2

    display some garbage value while subtraction in c prorgaming

    hi ,
    i am not much familiar with c programming.
    i had written a code given below
    Code:
            val1  = (double)Vanlg/Vref;
            val1 = (val1*2000)-2000;
            val = (unsigned int)val1;
                         d4 = val/1000 ;
                          r1 = val%1000;
                          d3 = r1/100 ;
                          byt = r1 % 100 ;
                          d2= byt/10 ;
                          d1 = byt%10 ;
    val1, val2 are double and val is unsigned int. d4, d3, d2 and d1 are display digit where value has been dislayed. according to calculation display should be zero or 1. but some time i get 534 value.

    Code:
            val1  = (double)Vanlg/Vref;
            val1 = (val1*2000);
    from the above two line i get value 1997, 1999, 2000.if i subtract 2000 from the above answer. then value are -3, -1, 0. but i am getting 534 and 0 . 0 is correct but what about 534.
    please go through it tell me whats going on above.
    thanking you..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    Post the smallest and simplest compilable program that demonstrates the error. If possible, the input should be hard coded into the program, otherwise you should at the very least tell us what is the input that results in the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    2
    Quote Originally Posted by laserlight View Post
    Post the smallest and simplest compilable program that demonstrates the error. If possible, the input should be hard coded into the program, otherwise you should at the very least tell us what is the input that results in the error.
    this lines of code i used in my embedded programing. input,
    Vanlg and Vref are in the range of 0 - 4095. after some trial i found that 534 which has been displayed on d4,d3,d2,d1 is for negative value.this is because i define the variable "val"as unsigned integer.
    can you suggest me how do i check negative number. if i take signed interger the my code not working properly.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    436
    Quote Originally Posted by abks View Post
    this lines of code i used in my embedded programing. input,
    Vanlg and Vref are in the range of 0 - 4095. after some trial i found that 534 which has been displayed on d4,d3,d2,d1 is for negative value.this is because i define the variable "val"as unsigned integer.
    can you suggest me how do i check negative number. if i take signed interger the my code not working properly.
    Yet, you didn't understand the @laserlight request to allow us to help you... In your code you have Vlang, Vref, val, val1, d1, d2, d3, d4, r1 and byt variables. You said val1 is a double (there is no val2 in your code) and val is unsigned int. Which are the types of Vlang, Vref, val1, val2, d1, d2, d3, d4, r1 and byt? What are the initial values of Vlang and Vref? You got 534 in which variable? You said d1, d2, d3, d4 are used to "display" but how? These are supposed to represent 4 decimal algarisms?

    I think you are getting Vlang from an A/D and Vref is the constant 4095 (hence if Vlang is in 0 ~ 4095 range, you got a 12 bits A/D), when dividing those values you want to get a value in the range 0.0 ~ 1.0. And in the second line:
    Code:
    val1 = ( val1 * 2000 ) - 2000;
    Your val1 will be in -2000.0 ~ 0.0 range (notice the signal). Is this really what you want? Or are you trying to convert ranges (from a 4096 range to a 2001 one? Notice the new range goes from -2000 to 0, including 0).

    I suppose you want some values lesser then 10 on d1, d2, d3 and d4. If so, and if you are using unsigned ints for val, d1, d2, d3, d4, r1 and byt, remember that since double has signal, when converted back to unsigned int (val), the compiler will use complement 2: -2000 will be 0xfffff830. If you wanted a new range from 0 to 1999 (2000 steps range) you failed...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtraction in C
    By Satya in forum C Programming
    Replies: 1
    Last Post: 02-23-2015, 11:36 AM
  2. subtraction of strings
    By Lindzylu in forum C Programming
    Replies: 4
    Last Post: 07-04-2011, 09:25 PM
  3. pointer subtraction
    By bhrugu in forum C Programming
    Replies: 10
    Last Post: 08-07-2010, 02:04 PM
  4. subtraction of two pointers..
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 08:32 AM
  5. Division By Subtraction
    By blacksnake in forum C++ Programming
    Replies: 19
    Last Post: 01-13-2007, 10:17 AM

Tags for this Thread