Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prague/ Q&A

Name: Anonymous 2013-11-19 12:18

Ask /prog/ anything.

Although don't expect an answer or even a good one.

Also, keep it /prog/ related.

Name: Anonymous 2014-01-10 19:33

>>155
Range of a signed byte is -128 to 127. In signed representation, if the first bit is "1", the operation becomes:

-(2n-1) + (value of n-1 bits of the number)

i.e.,


10000000 = -128 + 0 = -128
10001000 = -128 + 8 = -120
11111111 = -128 + 127 = -1


All 0s in a bitfield is always 0, even in 2's complement. But just 1 in a 1-bit number is equal to:


-(2^0) + 0 = -1


Or, more realistically, because the maximum value of a 1-bit field is 1, and you're forcing the compiler to represent its only available bit as the sign bit, it has no other bits to add to it, so it will always be between 0 and -1.

I wonder if doing this is undefined behaviour?

Name: Anonymous 2014-01-11 16:06

>>155
You are thinking of sign magnitude, where the most significant bit represents the sign of the number while the remaining represent the magnitude. In this scheme, the range of values for negative and positive numbers are equal, but there are two representations for zero, 10000000 and 00000000.

>>157-sama is using 2's complement, where -x is obtained by performing ~x + 1. That is, interpreted as an unsigned value, the bits are flipped and the number is incremented. Using this representation, there is a unique representation for zero, but since there is only one representation for zero, the number of positive values cannot be the same as the number of negative values, since these with the zero representation would give an odd number of total values, which is not a power of two. So there will be an extra positive value or a negative one. In 2's complement, the extra value is negative. This means there is a strictly negative number that is fixed under the absolute value function, so the result of absolute value isn't necessarily positive. Hopefully programmers use integers that are large enough to avoid this behavior.

Name: Anonymous 2014-01-20 4:31

I had an idea for a program... I was going to write a quick program that converted units like:
./convert 120 in mm

I planned on having an external text file where I could add definitions like
in 2.54 cm
cm 10 mm

My intention was not to require the text file to have every possible combination of conversions, but to be able to use multiple conversions in order to get the final conversion. I thought linked lists might be a way to do this. Something like:

typedef struct unit {
char *name;
unit **nodes;
double *factors;
} unit;

The problem is the linked lists wouldn't really be linear or circular, just arbitrarily linked together with some nodes linking to many others. And what's worse, some linked lists would be completely independent from others (different dimensions). Is there a way to search through linked lists like that without an infinite loop?

Name: Anonymous 2014-01-20 5:50

>>159
Thats not a linked list you're talking about but a graph. And there are plenty of (great) ways to search a graph.

Name: Anonymous 2014-01-20 6:28

>>160
Well it's not quite a linked list, but it's not a graph either.

ft may be linked to in, cm, mm, and miles. miles may be linked to ft, km, and m. Maybe m is only linked to km. nm may be linked to mm and light years.

There is a way to search through that without it infinitely looping through it?

Name: Anonymous 2014-01-20 6:33

>>161
I just realized I think this can be searched through easily, but imagine there were every more links that looked circular.

Name: Anonymous 2014-01-20 7:46

>>161
It's definitely a graph.

Name: Anonymous 2014-01-20 12:04

just base everything off the standard units? every unit has a base unit.

http://en.wikipedia.org/wiki/International_System_of_Units

so to convert mm to nautical mile you first find out how many mm to a meter (the std unit) and then convert meter to mile.

you could even do things like "./convert 10 miles per hour to km per week"

Name: Anonymous 2014-01-20 13:42

>>159
What, you mean like units?

$ units
Currency exchange rates from 2013-07-11
2562 units, 85 prefixes, 66 nonlinear units

You have: 120 inches
You want: millimeters
* 3048
/ 0.00032808399


You might look at how they do it.

Name: Anonymous 2014-01-20 21:11

>>165
Or frink.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List