Coder Social home page Coder Social logo

mescc's People

Contributors

miguelvis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mescc's Issues

Problem comparing integer values.

16 bit signed integers can be used to represent values between −32768 and 32767.

However comparing integers using the less than or greater than operators fails in some cases when when the difference between values is 32768 or more.

Greater than (>) returns an incorrect result in all cases when the difference between the two values is 32768 or more except when comparing -32768 and 0.

Less than (<) returns an incorrect result in all cases when the difference between the two values is more then 32768.

The following program demonstrates the problem:

/*
 * compare.c
 *
 * Copyright(C) 2020 - MT
 *
 * Compares several pairs of values and checks the results.
 *
 * This  program is free software: you can redistribute it and/or modify it
 * under  the terms of the GNU General Public License as published  by  the
 * Free  Software Foundation, either version 3 of the License, or (at  your
 * option) any later version.
 *
 * This  program  is distributed in the hope that it will  be  useful,  but
 * WITHOUT   ANY   WARRANTY;   without even   the   implied   warranty   of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You  should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 06 Jan 21   0.1   - Initial version - MT
 *
 * To Do:            - Figure out why this doesn't work!
 */

#include "mescc.h"
#include "printf.h"

#define  DEBUG    0

int min(m, n) int m, n; 
{
   if (m < n)
   {
      if(DEBUG)
         printf("min %12d, %d = %d (a)\n", n, m, m);
      return m;
   }
   else
   {
      if(DEBUG)
            printf ("min %12d, %d = %d (b)\n", m, n, n);
      return n;
   }
}

int max(m, n) int m, n; 
{
   if (m > n)
   {
      if(DEBUG)
         printf ("max %12d, %d = %d (c)\n", n, m, m);
      return m;
   }
   else
   {
      if(DEBUG)
         printf ("max %12d, %d = %d (d)\n", m, n, n);
      return n;
   }
}

int main()
{
   int a[15];
   int b[15];
   int i;
   
   /* The  comments next to each pair of values indicate which  comparisons
    * work and which do not. */
    
                                    /* min()/max() */
   a[1] = 0;      b[1] = 32767;     /*  Pass/Pass  */
   a[2] = 0;      b[2] = 16383;     /*  Pass/Pass  */
   a[3] = -16384; b[3] = 0;         /*  Pass/Pass  */
   a[4] = -32678; b[4] = 0;         /*  Pass/Pass  Δ 32768 */
   
   a[5] =  16384; b[5] = 32767;     /*  Pass/Pass  */
   a[6] = -32768; b[6] = -16384;    /*  Pass/Pass  */
   a[7] = -1;     b[7] = 32767;     /*  Pass/Fail  Δ 32768 */
   a[8] = -32767; b[8] = 1;         /*  Pass/Fail  Δ 32768 */
   a[9] = -2;     b[9] = 32767;     /*  Fail/Fail  Δ > 32768 */
   a[10] = -32767;b[10] = 2;        /*  Fail/Fail  Δ > 32768 */
   a[11] = -32768;b[11] = 32767;    /*  Fail/Fail  Δ > 32768 */
   a[12] = -16384;b[12] = 16384;    /*  Pass/Fail  Δ 32768 */
   a[13] = -8192; b[13] = 24576;    /*  Pass/Fail  Δ 32768 */
   a[14] =-16384; b[14] = 16384;    /*  Pass/Fail  Δ 32768 */

   for ( i = 1; i <= 14; i++)
   {
      if (a[i] == min(a[i], b[i]))
         printf ("%3d min(%6d, %-5d) = %-6d \tPassed\n",
            i, a[i], b[i], a[i]);
      else
         printf ("%3d min(%6d, %-5d) = %-6d \tFailed\n", 
            i, a[i], b[i], b[i]);
   }
   
   printf("\n");

   for ( i = 1; i <= 14; i++)
   {
      if (b[i] == max(a[i], b[i]))
         printf ("%3d max(%6d, %-5d) = %-6d \tPassed\n", 
            i, a[i], b[i], b[i]);
      else
         printf ("%3d max(%6d, %-5d) = %-6d \tFailed\n", 
            i, a[i], b[i], a[i]);
   }
}

This produces the following output.

  1 min(     0, 32767) = 0              Passed
  2 min(     0, 16383) = 0              Passed
  3 min(-16384, 0    ) = -16384         Passed
  4 min(-32678, 0    ) = -32678         Passed
  5 min( 16384, 32767) = 16384          Passed
  6 min(--.)*(, -16384) = --.)*(        Passed
  7 min(    -1, 32767) = -1             Passed
  8 min(-32767, 1    ) = -32767         Passed
  9 min(    -2, 32767) = 32767          Failed
 10 min(-32767, 2    ) = 2              Failed
 11 min(--.)*(, 32767) = 32767          Failed
 12 min(-16384, 16384) = -16384         Passed
 13 min( -8192, 24576) = -8192          Passed
 14 min(-16384, 16384) = -16384         Passed

  1 max(     0, 32767) = 32767          Passed
  2 max(     0, 16383) = 16383          Passed
  3 max(-16384, 0    ) = 0              Passed
  4 max(-32678, 0    ) = 0              Passed
  5 max( 16384, 32767) = 32767          Passed
  6 max(--.)*(, -16384) = -16384        Passed
  7 max(    -1, 32767) = -1             Failed
  8 max(-32767, 1    ) = -32767         Failed
  9 max(    -2, 32767) = -2             Failed
 10 max(-32767, 2    ) = -32767         Failed
 11 max(--.)*(, 32767) = --.)*(         Failed
 12 max(-16384, 16384) = -16384         Failed
 13 max( -8192, 24576) = -8192          Failed
 14 max(-16384, 16384) = -16384         Failed

printf() doesn't display -32768

An integer value of -32768 behaves as I would expect but is not printed properly using printf()

Example:

#define INT_MIN   -32768
#include "mescc.h"
#include "printf.h"

main()
{
   int i;
   i = INT_MIN;
   printf("INT_MIN= %d\n", -32768);
   printf("INT_MIN= %d\n", INT_MIN);
   printf("INT_MIN= %d\n", i);
   printf("INT_MIN= %d\n", i + 1);
   printf("INT_MIN= %d\n", ++i);
}

Output:

INT_MIN= --.)*(
INT_MIN= --.)*(
INT_MIN= --.)*(
INT_MIN= -32767
INT_MIN= -32767

Thanks for making the effort to keep small-C going!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.