simple question (signed ints)

This is the place for queries that don't fit in any of the other categories.

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 4:44 pm

Are those what you wanted?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 4:55 pm

not sure if ov( -250, 1 ) is right, but ov( -128, 1 ) is definately wrong...

other than those, everything's fine. :)

EDIT:
tested in calculator:
-250 should be -122
so yep, it right :)

EDIT2:
ov( -128, 1 ) is partially correct since ov( 128, 1 ) is 0 and the equation doesn't know the difference between them
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby stranac » Mon Feb 03, 2014 5:09 pm

If ov(-250, 1) should be -122, ov(-128, 1) should be 0.
If ov(-128, 1) should be -128, ov(-250, 1) should be -121.

I'm guessing you'll need some simple adjustments to get the result you want.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1218
Joined: Thu Feb 07, 2013 3:42 pm

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 5:15 pm

the confusion is the two's complement, where 0xFF is -1 instead of -0

what you've provided only allows up to -127 (0x81) for a signed 8-bit int (0x79 is 127, 0x80 is -128, and 0x00 is of course 0)

EDIT:
looks like my last working example was right after all... heh

I'll take it over to daniweb for optimization... I think I've caused you enough stress here...
sorry :oops:
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby Kebap » Mon Feb 03, 2014 9:27 pm

Maybe check out the link in my signature some time, to find out how to stress less and get your answers faster. :geek:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 400
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: simple question (signed ints)

Postby Tcll » Mon Feb 03, 2014 11:34 pm

Kebap wrote:Maybe check out the link in my signature some time, to find out how to stress less and get your answers faster. :geek:


nice article...
but in my defense (not saying what I did here was right in my approach), I'm autistic,
and typically don't think of alot of these things when trying to describe my issue...

honestly...
I thought it already would blow right through as I thought this was a well known area in binary programming... so that was my fault

to top things off, I usually am not sure how to explain something appropriately until after I have the answer...
(IK this causes alot of confusion on both of our ends, but it's like... I'd have to speak with my future self to know how to explain it now)

sorry I can be such a bother at times :(
I try not to be, and do work on it quite often... but I have my moments...

thanks for caring though :)
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby Mekire » Tue Feb 04, 2014 2:32 am

No clue what is going on in this thread:

You want signed ints that overflow back to negative or not?
Code: Select all
def overflow(number, signed=True, bits=8):
    bit_mod = 2**bits
    signed_mod 
= bit_mod//2
    if signed:
        return (number+signed_mod)%bit_mod-signed_mod
    else
:
        return number%bit_mod
        
    
if __name__ 
== "__main__":
    print(overflow(127))
    print(overflow(128))
    print(overflow(-250))
Code: Select all
127
-128
6

-Mek
User avatar
Mekire
 
Posts: 1032
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: simple question (signed ints)

Postby Tcll » Tue Feb 04, 2014 3:48 am

Tcll wrote:did a little renaming and reconfiguring

>>> ov( -250, 1 )
-122

>>> ov( 200, 1 )
72
>>> ov( -128, 1 )
-128
>>> ov( 128, 1 )
0
>>> ov( 127, 1 )
127


would what's in yellow be correct??

here's the code:
Code: Select all
def ov( value, byte_size ):
    sign = False
    if value<0: value += 1; sign = True
    value = abs( value ) & ((1<<((byte_size<<3)-1))-1)
    if sign: value = (value*-1)-1
    return value


EDIT:
a little math on 200:
11001000 & 01111111 = 72
so yes bs8( 200 ) is 72


this post already got it,
and I optimized it to:

Code: Select all
def ov(value,byte_size):
    sign = value<0
    return (((-1)**sign)*(abs(value+sign)%(1<<((byte_size<<3)-1))))-sign


it works perfectly :)

though now I have to ask if what I'm doing should really be considered "proper"?? >.>
since what I'm doing is basically the same thing as masking, but I'm carrying the sign-bit...
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Re: simple question (signed ints)

Postby casevh » Tue Feb 04, 2014 7:12 am

The question is ambiguous because -250 doesn't have a well-defined bit pattern. Most modern computer architectures use 2's complement binary format which doesn't have a sign bit. In 8 bit 2's complement binary format, you can store the numbers -128 to 127 (note there is one more negative value than positive value). Older computer occasionally used 1's complement binary format which uses a sign bit. In 8 bit 1's complement binary format, you can store the numbers -127 to -0, and 0 to 127 (note there are two versions of 0).

Assuming -250 is stored in 16 bit 2's complement binary format, the bit pattern is: 1111111100000110. If you just keep the last 8 bits (which is a completely valid approach to "down-sizing" a longer number, you'll get 6. But since there are other approaches that can be used, you'll need to decide what you are really trying to do.

casevh
casevh
 
Posts: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: simple question (signed ints)

Postby Tcll » Tue Feb 04, 2014 2:14 pm

casevh wrote:Most modern computer architectures use 2's complement binary format which doesn't have a sign bit.


o.o
that's not what I read when I designed the signed-int r/w functions in dev4x of my program, even though I didn't consider it... >_>
oh where is that reference... <.<
(will edit when I find it)

yea, the reason I'm so up-tight about these standards is because I'm re-evaluating everything.
dev5 introduces a pointer system which needs all this extra care...

sure I could just make it throw an error, but my rep aims to step things to the next level.

in dev4x, since the input size was known, it was easy to just add a half-size value to the number and break down the binary.
but in this system, the way we need to write the value would require supplying an additional byte_size before we could break the value down properly.
so I used the next best approach, which was getting the mantissa and sign-bit, and fitting the mantissa in the new bit-width and applying the sign-bit.

I'm gonna need to do this for floating point values as well >_>
I think I'll just take the simple approach and use the breakdown method for writing a float of the required byte-size.
anyways... that's out of the question... we're talking about signed ints... not floats :P

there's another idea I had about ranging the value to shrink the binary into the new format...
(Nintendo does this with int vertices and applies an exponent to a two's complemented equation using the int (a pseudo-float))
maybe I'll just use an external library to managge that... heh

my original idea for dealing with overflow was to max -250 between -128 and 127 for s8 ints. (result would be -128)
User avatar
Tcll
 
Posts: 100
Joined: Wed Jan 01, 2014 6:36 pm

Previous

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests