Intel's Ronler Acres Plant

Silicon Forest
If the type is too small, Ctrl+ is your friend

Showing posts sorted by relevance for query FAREY. Sort by date Show all posts
Showing posts sorted by relevance for query FAREY. Sort by date Show all posts

Friday, June 8, 2018

Bugs

I've been working on my Farey addition program and I found a couple of bugs. The first one a simple mistake that took me four days to find, mostly because my mind was a bit fuzzy. The problem was that I was using abs (absolute value function for integers) instead of fabs, which is the same function, but for floating point numbers. One little letter and everything is wrong.

Got that corrected and now I'm running the program and when the denominator gets to 4142 it goes off the rails. What the heck could be causing that? It's been working fine for the first 4141 denominators, why should it choke on 4142? It blows up when it is checking the Farey addition. Doing this only involves integer operations, and they are all relatively small integers, we aren't going to overflow the accumulator. What could possibly be go wrong? This one had me stymied for a couple of days, I couldn't even think of what to look at. There is nothing wrong except it doesn't work.

This morning my brain served up a clue. When I generate the fractions, I compute their decimal value and use that to sort my list of fractions. The problem is that I also depend on this value being unique. If two fractions have the same decimal value, I presume they are duplicates and eliminate one. The problem might be (I haven't verified it yet) is that two fractions could have the same decimal value, but be different. For instance, Google delivers these values:
2048 / 4007 = 0.51110556526
2071 / 4052 = 0.51110562685
2117 / 4142 = 0.51110574601
The first 6 digits of these three fractions are all the same. After that they diverge. In my debug output, they all show the same value, but then I am only printing the first six digits. Standard floating point values hold much more than six digits, so maybe there is something else going on here. Anyway, I've got a place to start looking which is more than I had a couple of days ago.
 

Friday, June 1, 2018

Tangent Circles

Fun with Circles
I came across this bit of geometry on Quora the other day. I haven't quite sorted out just why it works, but if it does, it's pretty cool. I was so impressed with it that I printed a copy and took it lunch to show the gang.


Funny Fractions and Ford Circles - Numberphile

Dennis responds with this video, which also has a bunch of tangent circles along with some goofball math. I saw this and thought that it wouldn't be too much trouble to write a program to verify what's going on here, so I did. Turns out there were a couple of tricky bits that needed sorting, but I think I have it. The first tricky bit was figuring out how much memory I would need. I wrote about this a couple of days ago.

int gcf(int m, int n)    // greatest common factor
{
    if ((m==0) || (n==0))
    {
        if ((m==0) && (n==0)) return 1;
        if (m==0) return n;
        return m;
    }

    while(m!=n)
    {
        if(m > n)
            m -= n;
        else
            n -= m;
    }
    return m;
}

The next was figuring out how to find the greatest common factor (GCF) of two integers. I've run into this problem before, but where oh where has that bit of code gone? I dunno, but Google finds an example, but it doesn't work. I have to spend several minutes monkeying with it to get it to behave.

That was enough to verify that the Farey addition of fractions works. That is, you generate all of the fractions between zero and one using all denominators from 1 to whatever. Now take any three adjacent fractions on the number line. Add the numerators of the first and last and you will get the numerator of the middle one. Do the same for the denominator and you get the denominator of the middle fraction. You might have to reduce the fraction to make it identical, but the value will be the same regardless.

Verifying that you could use these fractions to generate tangent circles took a little more doing. One way to do it would be to check this out for every new denominator, since at that point the fractions on either side would be the ones you would be forming tangents with. I didn't want to do that, mostly because I was already generating all of the fractions prior to checking the Farey addition, so I need some way to keep track of a fractions "parents" even after multiple fractions had been interposed between them. What I finally settled on was, after generating all fractions for the next denominator, I recorded the values of the parent fractions. Then later I would use these values to locate the original fraction and verify that the generated circles would indeed be tangent.


Tangent Circles and Pythagoras

Verifying that the circles are actually tangent to each other is done by comparing the sum of their radii with the distance between their centers. If these two values are equal they are tangent. If the distance is larger, they are not touching. If the distance is smaller, they overlap.

The distance between centers can be calculated using the Pythagorean Theorem. All you need is the horizontal distance, which is simply the difference between the values of the two fractions, and the vertical difference, which is the difference in their radii. See the above illustration. The orange and purple lines form the sides of the right triangle and the black line forms the hypotenuse.

I fired up my program around 12 hours ago. I gave it some big number to work with, like a 100,000 or something. It has generated over 20 million fractions and it is still running. It seems to be marching on regardless of whether the desktop goes to sleep, or if I am using the computer. I am debating whether I should cancel it or let it keep running. If I remembered what the number was that I gave it, I could estimate how long it is going to run, but I just typed in a one and bunch of zeros. I suppose I should let it run just to make sure it doesn't crash before it finishes.

I've uploaded the source to github if you are interested. I intend to clean up the output so it gives a better picture of what it's doing. When I have done that I will update github.

Wednesday, May 30, 2018

Calculating

I'm working on a little program to play with some numbers and I need an array of the appropriate size. It's easy to do, you decide on some array size and then you ask the O.S. for a chunk of memory that big. If you are a well behaved program, and the O.S. likes the cut of your jib, he might let you have it.

You don't want to ask for anymore than you need because if you ask for a great deal, that might interfere with whatever else you are trying to do, like trying to watch YouTube videos. Also, large amounts of memory can involve lots of processor time, and you might not want to run the program for that long, so you run on it a smaller set of data.

In the C programming language there are a couple of ways to do this. The traditional way is call malloc, the O.S.'s memory allocation procedure with number of bytes you want. It will return a pointer which you will base your data structure on.

    fraction_s* a = malloc(q * sizeof(fraction_s));

Another way is to declare your array in the main procedure (which is where the program starts), and use another variable as the size of the array. You need to assign a value to the size variable before you try and make use of it in the declaration of the array. Get all that? Sounds kind of convoluted. Maybe it's just my explanation is convoluted. Let's see if a bit of code helps.

int main(int argc, char** argv)
{

    int q = 1000;    // default value
    if (argc>1)
        q = atoi(argv[1]);    // pick up value from command line

    fraction_s    a[q];       // declare an array of data blocks

    memset(a, 0, sizeof(a));  // zero out the array

Last thing is to fill the array with zeros. Saves having to do it explicitly whenever we are loading data, we can just skip all those locations for which we don't have data. That way, come run time, we can be sure we won't misinterpret them for data. We might want to set a flag in each block that does have valid data.

Took a while to get here, but we have finally got to the crux of the matter. I'm working on a program to check out Farey Addition of fractions. I need an array to hold of all the possible fractions that can be generated using any pair of integers, up to some limit. For instance if my limit was 1,000, I would need an array to hold a million numbers. If I confine my self to proper fractions (the numerator is smaller than the denominator), then I only need half as much. Roughly.

You don't want to get a size calculation like this wrong, because while it might be a nuisance when the program is only going to run for a few seconds, it would be the shitz if your program got a memory fault after it had been running for a week and it lost all your work, all because you got the memory request size calculation wrong.

You screw up once or twice and you start taking pains to get it right. Getting it right also insures that your program doesn't get kicked off for asking for too much memory, when a properly sized request would allow you to run. It also tells you how much memory you need for the kinds of problems you are working with.

There's probably a name for the series that tells you how many fractions you are going to generate. Fibonacci or Bernoulli, or maybe, I dunno, Flatulence? I think I'm getting burned out on the math section of Quora. Seems like half the questions over there are talking about some obscure thing named after an even more obscure, old, dead, white guy (usually). Numberphile does that some too, but usually it's just kind of a decoration, like ribbons and bows, on their presentation. On Quora, these names are in these questions and understanding what the term means is crucial to understanding the question. Sometimes it's really simple, like this fraction thing I am working around to explaining, which is great, it means I can understand the question. But sometimes it's such an esoteric term that only the half dozen people who are writing theses on it have any idea what it means. I'm not going into those ratholes, I have plenty of my own, thank you.

Back to our array. For a denominator of 1 you can have two values in the numerator: 0 or 1. We're only going to count the zero. One over one is one, which really isn't a fraction. We aren't going to count the one, so for one, we have one value. For a denominator of 2 you can have two real values: 0 or 1. Plus 1 for zero gives you three. Like so:

Denominator /
Number of Proper Fractions    Total
            1              1
            2              3
            3              6
            4             10
            5             15
            6             21
            7             28
            8             36
            9             45
           10             55

It kind of looks (n+1) * n/2. It looks an exact calculation to me. No issue with rounding due to division by 2 (of n and (n+1) ), one is going to be even and the other will be odd, so the product will always be even and divisible by two.

We could eliminate the zeros, and maybe I will, all zeros are alike, am I right? But right now it's handy becuase I've been using zero based arrays for so long it's like second nature. Taking out the zeros, oh boy, I dunno if that's ever been done before Mr. Halliday (figment of my imagination, my inner Jimmy Stewart talking to big fat boss man with the straw hat, bifocals and a cigar).

Sunday, November 21, 2021

More Pie


Why do calculators get this wrong? (We don't know!)
Stand-up Maths

Sometimes I like Matt Parker and sometimes he's just a little too much. I found this video to be pretty entertaining.

I ran a couple calculations on Google just to verify that this isn't just all horse puckey, and it seems to check out.

11^6 / 13 = 136273.923077

I put up a couple of posts about Farey Additon a couple of years ago.