Category Archives: C++ programming

Playing with pebbles

Today I started adding stubs to the structure of matter section of physics. It’s important to get to these structure of matter stubs, because these are on the boundary between chemistry and physics, and will let me start to connect the two sciences.

I created a new ChemSubst class, which is supposed to be a parent class for ChemElement, but it has no detail yet.   I added a new function to my graphics Object class, and expanded a little development of Arithmetic.  I have added (but not yet tested) predecrement and postdecrement  operators for the Tiny Whole Numbers class, and an addition operator for Tiny Whole Numbers.  I want to rewrite the test driver functions to refer to Tiny Whole Numbers. I would also like to include a calculator program: I have done a version of this before and could do it again, but I need to get back to the books and continue the programming exercises.

 

Collision test

I finally sat down and worked through the collision test function of  physical systems. In the process, I discovered and fixed a few bugs and added a couple of desirable features, such as tweaking the display of objects and including reminders of which object in a system I am working with.  Before I can make much more progress in physics, I need to work on some mathematical functions, and go back to review of the programming texts.

Divided up

I got most of my basic physics functions divided up and separated out. Physics is divided into subfunctions: The first is mechanics, which deals with motion of bodies and its causes.  The simplest subdivision is particle mechanics which deals with particles in which internal motions and rotations are unimportant.  The simplest division of particle mechanics is mechanics, which deals with the motion of particles regardless of their causes. So far, Kinematics includes position and velocity. I want to include acceleration, but this requires more mathematics. The next division of particle mechanics is dynamics: The only function I have associated with this involves setting mass. This will eventually include considerations of momentum, impulse, and force.  Energetics will involve work and kinetic energy, which also needs more work in mathematics. Particle systems include my examination of collisions

Rigid-body mechanics, deformable body mechanics, and gravitation require better development of systems.  Thermodynamics, electromagnetism, and the structure of matter are present, but I still need fundamentals of mechanics.

Physics Systems Setup

I decided to concentrate my efforts on the setup of physical systems, and gather the procedures into functions that can be called from other places in the program. This should leave me free to concentrated on other parts of the program.  The next step will be to do the same for the procedures of mechanics.

Inch by inch

I have been throwing most of my limited energy into expanding the demonstration program.  Most of the functions I have in system setup are or need to be duplicated in other areas, mostly mechanics. Some of these can be distributed to specific areas of mechanics, and several developments belong specifically to particle mechanics.  I cannot make much progress in other sciences without a solid foundation in physics.

However, in order to make progress in physics, I need to make more progress in mathematics, so I am continuing to develop room for the tiny whole numbers, approximate arithmetic, and algebraic functions.

 

Slow going

I’ve had a hard time being able to concentrate on programming for the past few days.  I think I have resolved the naming problem, but in the physics section, the mechanics interface is still clunky. Moving a couple of the setup routines into distinct functions created some linker errors, because of the various assumptions involved, but I have those cleared up.  I also have stubs for three of the four primary subdivisions of mechanics, and I am ready to start including functions for thermodynamics, electromagnetism, and the structure of matter.  Chemical substances is set up to include more stubs, and so is Personal Studies.

 

Troubles by the numbers

I did some more review and testing over the past couple of days.

The interfaces for my physical system set up and mechanics functions are awkward and clunky, and I need to experiment to get them right.

I found that I have a problem with adding and removing objects. The problem comes with the automatic naming: If I remove an object and then add another, the numbering is off and I can get objects with duplicate names. No, no.

I began writing the collision testing function to calculate relative positions and velocities of objects, and found that I have a problem when two objects are adjacent and approaching each other. Their relative velocity zooms them past each other without colliding. This is unacceptable, but I need to work on continuous movement with velocities greater than one in order to handle it properly.

I have begin writing stubs for higher functions of mechanics: Many of my current ones will go into particle mechanics, but there is some need to go into other divisions, especially since I have begun considering objects of larger size. The thermodynamics, electromagnetism, and structure of matter functions should now theoretically be able to add subfunctions and go through a cycle to get commands, but this is not yet properly tested.   Higher order processing will have to wait a few days.

 

New toys

In developing the demonstration program, an important part of handling collisions among objects is determining the relative position and velocity. This is an easy enough calculation to put in the show system function, for now.  The next step will be to examine the move object functions to handle collisions better.   I want to divide up the “Mechanics” section into areas; some of these will become more important as I increase the size of systems.  I am also adding the possibility of using multiple commands for the thermodynamics, electromagnetism, and structure of matter functions.  I am also starting to use multiple commands for chemical substances  and introducing sub-areas for personal studies.

I have also included a “Graphics Object” class which lends itself to some fairly complicated operations and manipulations. So far,  it consists of one pixel, set on or off. As with physical systems, I will be expanding this one element at a time, but it should be simpler than the physical system and go faster.

One of my mathematical idiosyncrasies is the handling of errors in arithmetic. Since computers typically represent numbers with finite and fixed combination of digits, which are not easily expandable, and have a hard time reporting both results and errors, I have decided to represent numbers as classes including a set of error flags and a value, and have the relations and operations on the numbers both return the error flags and set the values. I am working on the TinyWholeNumbers  class as an exercise in how this is to be done.  This class includes three flags: Undefined, which is used in the case for things such as division by zero, and results of illegitimate or doubtful comparisons and operations; underflow, for numbers less than zero, and overflow, for numbers greater than zero.  Values are in the range 0-255.

I have experimented with two different ways of reading and setting the flags: one won’t compile, and the other works but is wasteful of space. I am going to try a third way, typically used in Windows programming, (using an unsigned character to represent a bit and using masks and logical operations to clear and set bits).   It’s easy enough to create a TinyWholeNumber constructor to clear all the flags and set the value to zero.  An integer data type as used in C and C++ typically uses 4 bytes: which is much too large for the tiny version, and includes negative values.  To do the conversion, I need to throw away all negative values and set the underflow flag; or throw away all values larger than 255 and set the overflow flag.

Writing the assignment operator is also tricky. I regard trying to set a number to an illegitimate value (one that has an underflow, or overflow flag set) as a questionable operation, and so it sets the “undefined” flag.

When I output a number, I append a character corresponding to one of these flags so that I can tell whether it came from a good calculation or a questionable one.  This will be important in higher orders of mathematics, when errors can propagate, proliferate, and corrupt a calculation. I need to let most calculations proceed, since the results will be good in most cases, but I also need to be able to trace errors to their source.

Mush head

I now have “getter” and “setter” functions for the attributes of physical objects, but I still have some work to do. I need a “reset position” for objects in a physical system, which looks to be a little complicated. I need to remove the object’s imprint on the system map (because the new position may overlap the old one),  test to see whether it can be added in a new position (because of the system boundaries and interference with other objects), and either add it to the new position, or keep it in the old position if it cannot be moved.

I added a couple of stubs for physical geography and organism biology, and attempted to do something with processing of graphic objects, which somehow introduced a bug.  I did manage to introduce a section on approximate arithmetic (rounding), which suggest more functions later.

The work on operator overloading, inheritance, stream i/o, and file processing went slow, and I found that I could create a test file for the file I/O examples, but couldn’t read it.  Somehow, my programming brain turned to mush today.  I need to do something to sharpen it back up again.

Doing the rounds

I have more or less incidentally added a function to set the electrical charge of an object.  I’m not doing much with it, yet, and so far my charges are limited to +1 and -1, but it’s there.    I have successfully also managed to add a function to my physical system set up where I can change the size of an object. I wouldn’t normally want to do this except for testing purposes, but it needs to be there.  The default size of an object on creation is now 1, so I need to be able to change it. It works so that I can’t expand an object to larger than the system or if there is crowding by a neighbor, but I can expand and contract them.  I have  a stub for atmospheric science and one for cell biology.

The sections on operator overloading, stream input/output, random access functions, inheritance, and polymorphism are going well. I have moved from structures into the wacky world of bit fiddling. This will be important along with the operator overloading when I go more deeply into arithmetic.