Thread: How efficient do you think is this Int To String Converter Function?

  1. #16
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by babaliaris View Post
    Nice tip!!
    Yeah. Except it doesn't actually do what it's supposed to do :/

    Edit: viz, eliminate undefined behaviour is what the tip is, I assume, intended to do but it doesn't
    Last edited by Hodor; 12-26-2017 at 04:59 AM.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Because the negative side of the integer has more numbers than the positive one, about the only thing you can do to switch the sign safely is use a bigger type.
    One notices that there are no problems with this example...
    Code:
    #include <stdio.h>
    #include <stdint.h>
    #include <limits.h>
    int main(void) {
       printf("%lu\n", -(uint64_t)INT_MIN);
       return 0;
    }
    Last edited by whiteflags; 12-26-2017 at 06:13 AM.

  3. #18
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by Hodor View Post
    Yeah. Except it doesn't actually do what it's supposed to do :/
    Edit: viz, eliminate undefined behaviour is what the tip is, I assume, intended to do but it doesn't
    Hodor is right, but only because I (obviously) made a mistake, due to haste. It should be:
    Code:
    void print_int(int n) {
        unsigned u;
        if (n < 0) {
            u = (unsigned)(-(n + 1)) + 1u;
            putchar('-');
        } else
            u = n;
        print_uint(u);
    }
    Last edited by john.c; 12-26-2017 at 11:23 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #19
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by whiteflags View Post
    Because the negative side of the integer has more numbers than the positive one, about the only thing you can do to switch the sign safely is use a bigger type. One notices that there are no problems with this example...
    There's nothing wrong with the corrected version of my example.
    And since you will eventually run out of bigger types, the unsigned trick is generally better.

    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making function smaller and more efficient.
    By cmajor28 in forum C++ Programming
    Replies: 20
    Last Post: 03-19-2015, 09:03 AM
  2. More efficient way to structure this function?
    By Justin H in forum C Programming
    Replies: 8
    Last Post: 10-20-2012, 12:28 AM
  3. more efficient prime function??
    By codingGuy in forum C Programming
    Replies: 10
    Last Post: 10-12-2011, 05:09 PM
  4. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 06:06 PM
  5. Binary Converter - Various stupid string issues :p
    By polarpete in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2005, 02:46 PM

Tags for this Thread