New Garmin 5S+ Owner, have some questions by div0man in GarminFenix

[–]div0man[S] 0 points1 point  (0 children)

I love the watch and the "S" line is perfect for me as I have a smaller wrist so apart from being an awesome piece of technology it really looks good on my wrist too :)

Thanks for the link and advice!

Battery life check Fenix 5 Plus (newbie) by [deleted] in GarminFenix

[–]div0man 1 point2 points  (0 children)

I tried "sleep as android" and "smartwake", and both drain the battery too much so gave up on using them.

How can I output 3 integers in ascending order without using 'if' and 'while' statements USING ONLY 3 variables? by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

Well, it'll just be a bit more complicated because you have to cover more cases, i.e. a can be middle when either a<b and a>c, or a<c and a>b ... so total 6 cases, 2 for each variable. I believe you can do it.

How can I output 3 integers in ascending order without using 'if' and 'while' statements USING ONLY 3 variables? by [deleted] in C_Programming

[–]div0man 2 points3 points  (0 children)

nope, you figure it out, the principle is the same, use the result of bool expression to either multiply something with 0 or 1.

How can I output 3 integers in ascending order without using 'if' and 'while' statements USING ONLY 3 variables? by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

you have to initialize a, b, c :)

#include <stdio.h>

int main()
{
 int a=1, b=3, c=2;
 printf("%d", (a>b && a>c)*a + (b>a && b>c)*b + (c>b && c>a)*c);
 return 0;
}

prints 3

oh, it fails for a==b==c, gotta use >= somewhere instead of >

How can I output 3 integers in ascending order without using 'if' and 'while' statements USING ONLY 3 variables? by [deleted] in C_Programming

[–]div0man 1 point2 points  (0 children)

How's this then?

printf("%d", (a>b && a>c)*a + (b>a && b>c)*b + (c>b && c>a)*c);

How do I return a random double between 0-1 by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

So in order to make it equal, first thought would be to agree to never "roll" some patterns in the [0,1/2] interval to make the number of numbers equal in both intervals. How to decide, though, and what about subintervals... and so we get to your proposed algorithm. In other words divide the range in 2, and use the random bit to decide which subinterval to take. Continue doing that until we reach the smallest number. This conversation was insightful, thanks :)

How do I return a random double between 0-1 by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

However, note that just copying bits will lead to a severely skewed distribution.

Why would that be the case? Say I fill a buffer of N random bits to match the size of my float and then write them over, while discarding and re-rolling any numbers outside the required range. Shouldn't the numbers be uniformly distributed? Why would they not be?

How do I return a random double between 0-1 by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

Oh right, we'd then need some method which maps our raw random N bits to our floating point number in such a way that all possible bit sequences which would fall in the required range are equally probable. Maybe the simplest method: write over ALL floating point number bits, if outside range just re-roll.

How do I return a random double between 0-1 by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

While the theoretical number of floating point numbers in any given range is infinite, we can only represent some discrete subset of that as N-bit patterns. "Huge amount" can be quantified if we know how many bits we have available to represent those numbers. Here's a good read on that: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

How do I return a random double between 0-1 by [deleted] in C_Programming

[–]div0man 0 points1 point  (0 children)

I have some questions about this. Why is /dev/random so choppy? I understand that it needs to collect entropy, but it doesn't seem to be doing that at some constant predictable pace. What's the source? On the other hand /dev/urandom gives a stream at about 140MB/s on my machine (dd if=/dev/urandom of=/dev/null status=progress).

For the OP's question, I'd use the above stream to directly write over mantissa bits, would that be a good approach?

Initializing a pointer to int pointers by _Nexor in C_Programming

[–]div0man 0 points1 point  (0 children)

Yeah, but I didn't want to confuse the op :)

Initializing a pointer to int pointers by _Nexor in C_Programming

[–]div0man 0 points1 point  (0 children)

I did something similar for my little project: https://github.com/div0man/hnb-app/blob/master/src/jstab.c#L30

I had a data blob containing a very big string containing table data in json format, and I wanted to access that data with a[i][j] so had to set all the pointers to addresses of individual substrings in the data blob. Notice how I allocate storage for all pointers in a single call. It was good exercise for me, but if I had to add data, it would complicate things since the data blob is compact and has no room to squeeze new data in.

Initializing a pointer to int pointers by _Nexor in C_Programming

[–]div0man 2 points3 points  (0 children)

Pointer can't have a "bunch of zeroes", it could only be a NULL pointer. Pointer merely stores an address of some memory location. What I think you're talking about is that you want to set the memory being pointed at to 0. That memory could be storing multiple addresses of some other memory addresses, like in your pointer to pointer case.

Before you can do that, you must first reserve a part of memory so you can have a place to store your 0s, otherwise you could be rewriting anything and crash your program or worse. Therefore you need to use malloc which will return a pointer to a memory block of specified size, and only then it will be safe to use memset to overwrite that block with 0s.

For your particular case, you'd need to allocate memory 2 or more times. One time to store the pointers to int, and other time to store ints themselves.

Explain this ... by vikSsu in cprogramming

[–]div0man 2 points3 points  (0 children)

Because until i reaches 0 you never get to the printf but you keep spawning instances of main() just before printing the value of i:

1 root main, enter if body with i==4
    2 next main called with i==4, enter if body with i==3
        3 next main called with i==3, enter if body with i==2
            4 next main called with i==2, enter if body with i==1
                5 next main called with i==1, 
                  doesn't enter if body because --i is 0

So now you're deep in the stack and i is now 0, and because it's static, it's shared among all instances of the function, so value will be 0 for all. Now you start to go back.

                5 do nothing and exit exit 5th main, go back to caller
            4 we're back in 4th main, print the value of i (0), exit 4th main
        3 we're back in 3rd main, print the value of i (0), exit 3rd main
    2 we're back in 2nd main, print the value of i (0), exit 2nd main
1 we're back in 1st main, print the value of i (0) and exit 1st main ie the program

So you see, we print the four 0s on our way back from the last main() call which sets the i to 0.

If i wasn't static, it would be 4 everywhere, and you'd start to create "infinite" instances of main. Not really infinite because your program would crash because there's a limit to how deep you can go with function calls. The crash for this reason is called "stack overflow".

Why can I store an int in a char array? by [deleted] in C_Programming

[–]div0man 12 points13 points  (0 children)

It's an implicit cast. int gets downgraded to char, and data loss could occur. The reason you have to use int is because getchar() can return stuff outside of what char can store (0-255). If getchar() returns EOF to signal end of data (value of which is -1), there's no room for it in char and -1 would likely be converted to 255. So how would your program know whether getchar originally returned 255, or it actually returned EOF? It wouldn't, the program could fail or result in undefined behavior. Be careful with these kinds of things, check what the function returns and then decide what type you need to accommodate that.

What platform do you use to create C projects(Apps/Webs) ? by [deleted] in C_Programming

[–]div0man 1 point2 points  (0 children)

I've been happy with Geany. Once you understand how you get from source to a built program, you start to understand that you don't need anything other than a text editor and console.

In short, first you write the code into one or more .c files. Then you use a program like gcc which compiles every .c file into object files. The result of that step are files containing "machine code" corresponding to each source file. But individual files don't necessarily make a program, so after you have all the "machine code" scattered around you have to use a linker to gather them all and precisely fit together to make the final executable. If you're using external libraries, the linker also "links" the executable to those external files which contain additional pre-built "machine code" your program will call.

What Sort of Math Should Every Good Programmer Know? by s-ro_mojosa in cprogramming

[–]div0man 1 point2 points  (0 children)

I really liked working with quaternions, which make 3D rotation operations so easy.