arithmetic help

This is a discussion on arithmetic help within the C Programming forums, part of the General Programming Boards category; Hi, I want to make a programming procedure a bit more compact if possible: I have the following code: Code: ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    20

    Question arithmetic help

    Hi,
    I want to make a programming procedure a bit more compact if possible:

    I have the following code:
    Code:
    available = QUEUE_SIZE - sendQ.count;
    
    if(available < length)
    	copySize = available;
    else
    	copySize = length;
    but want to reduce to a singal line:
    Code:
    copySize = ???
    All I want is that the variable copySize always land a value between 0 and (QUEUE_SIZE - sendQ.count).

    is it possible?

    Thanks,
    Indy
    Last edited by freeindy; 09-18-2007 at 07:41 AM. Reason: clarifying...

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think this might do it:

    copySize = (available < length)? available : length;

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by freeindy View Post
    Hi,
    I want to make a programming procedure a bit more compact if possible:

    I have the following code:
    Code:
    available = QUEUE_SIZE - sendQ.count;
    
    if(available < length)
    	copySize = available;
    else
    	copySize = length;
    but want to reduce to a singal line:
    Code:
    copySize = ???
    is it possible?

    Thanks,
    Indy
    you mean something like:
    Code:
    copySize = (available < length)?available:length;
    This will make it more compact, but only in number of lines. The code generate by the compiler will be exactly identical. The sampe applies if you inline the calculation of
    Code:
    available = QUEUE_SIZE - sendQ.count;
    to:
    Code:
    copySize = (QUEUE_SIZE - sendQ.count < length)?QUEUE_SIZE - sendQ.count:length;
    All you've achieved now is to make the code less legible - and if you are REALLY UNLUCKY, the compiler will do the subtraction to calculate the "available" twice, which makes the code slightly slower. Unlikely, but possible.

    I think the original code looks just fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    ah yeah ofcourse... i was sitting and trying with &#37; + / *.
    thanks mike_g

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your clarification doesn't change anything, you still need to check if "available" is larger than length, in which case you should only copy length - or isn't that the case in your code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > I want to make a programming procedure a bit more compact if possible:
    Whilst you can do this, the readability and maintainability of the result suffers as a result.

    matsp is right, there is nothing wrong with the original.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As I explained, it makes it a few lines shorter to use a conditional assignment, but the compiler should come up with exactly the same thing.

    You could use % to make sure the size is OK, but it's not going to make it faster, even if it's shorter.

    Making code "obvious" is a good goal. If you don't want to do that, then fine, but if it's not improving anything and making the code less readable, then I don't see the point at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    20
    i know about the readability/maintainability. the first reason was to use only arithmetic operation is for faster execution on the target machine which is not a pc. It's an atmel processor with a speed of 4 MHz i think. I thought it would be good to use faster execution method. But you're right. I should leave it to at it is.

    Thanks, Indy.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I always thought obfuscation was one of the best things about C. When no-one else understands it you look clever :P

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mike_g View Post
    I always thought obfuscation was one of the best things about C. When no-one else understands it you look clever :P
    I know this is supposed to be a joke, but: Yeah, right, except when you have to spend half your day every day explaining what the code that you wrote does, rather than writing new code.... The above principle is fine if you are the only one writing the code, but not if someone else is supposed to use the code (or you need to go back and fix bugs two years later when a customer with a special use-case needs something fixed that was never tested in the initial release).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, its just a joke dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointer arithmetic and types
    By brooksbp in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 01:53 PM
  2. GNU Multiple Precision Arithmetic Library Problem
    By parad0x13 in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2008, 04:16 PM
  3. Replies: 3
    Last Post: 05-01-2003, 12:47 PM
  4. Pointer arithmetic
    By freebu in forum C Programming
    Replies: 4
    Last Post: 04-27-2003, 09:27 AM
  5. Replies: 1
    Last Post: 11-19-2001, 03:45 PM

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