It looks like you're using an Ad Blocker.

Please white-list or disable AboveTopSecret.com in your ad-blocking tool.

Thank you.

 

Some features of ATS will be disabled while you continue to use an ad-blocker.

 

A guide to programming - Part 1: Variables

page: 1
13
<<   2 >>

log in

join
share:

posted on Apr, 23 2021 @ 06:34 AM
link   
Often when a person first learns to program they focus on a single programming language, which is a perfectly fine way to start out, but it prevents you from understanding the common programming paradigms that are found in almost any language. Now that I'm experience with many different languages I'm able to see the similarities between them all, and I can read a new language I've never seen before without much effort because I understand the basic ideas that underpin the majority of programming languages.

In this first part of my guide to programming I will start by explaining variables and give code examples in C++ and JavaScript to show how the same basic concepts apply to both languages. C++ is often described as a complex language which is hard to learn, but that isn't really a valid statement in my mind, because JavaScript is in many ways more complex and more difficult to understand since it allows a lot more flexibility. C++ is simply more terse and requires variable types to be strictly defined, so it seems harder to learn.

This is why I think it's so important to start with variables when you are first learning to program. Without a proper understanding of variables you wont be able to understand what your program is really doing. Essentially all programming languages have a very similar set of variable types, which usually include integers, floats, chars, strings, and booleans, which are the 5 main types I will focus on in this first part of my programming guide. Even though JavaScript doesn't make you specify a variable type, it still uses a few different types.

For example consider this JavaScript code:

var myVar1 = 123.456;
var myVar2 = 100;
var myVar3 = myVar1 + myVar2;

And some C++ code to do the same thing:

float myVar1 = 123.456;
int myVar2 = 100;
double myVar3 = myVar1 + myVar2;


Here we are declaring 3 different variables, hopefully it is obvious we are summing the 1st and 2nd variables and saving the result in the 3rd variable. In C++ we need to declare the variable types (a float, double, and integer in this case) however JavaScript lets you create a variable with the var keyword very easily. JavaScript will actually use different types of variables depending on what data you put into the variable. This is very important because if you don't know what variable types your code is really using it can lead to all sorts of bugs.

In reality JavaScript will use a double (aka double precision floating point number) to store numerical values. A floating point number basically means a number with decimal places, an integer is a whole number which doesn't require decimal places. In C++ a "float" is a 32 bit floating point number, a "double" is a 64 bit floating point number, which means it requires twice as many bits but can store more decimal places, allowing for more precise calculations. An "int" is a 32 bit integer, and I used all 3 of these types for an important reason.

In C++ when you add an integer to a floating point number the integer will first be converted to a float, so myVar2 becomes a float, and the sum is a float, but then we save it into a double, which is also a float but just has more bits (64 bits), so C++ converts the 32 bit float into a 64 bit float and saves the result in myVar3. I know this may sound complicated at first glance but you will learn how these basic variables work fairly quickly.

The most important thing to understand here is probably the bits/binary which make up each variable. This is something I wish I had of understood when I first started programming. It's crucial to understand that every variable you create is really just a bunch of binary data stored in memory. When I first started programming I had this notion that string variables (aka text) were somehow different to number variables like integers and floats.

The reality is everything is made of bits, text is simply a series of characters, and each character is represented by a number, and those numbers are stored as binary data. For example lets say we have a 2 bit integer, that means it could hold 4 possible values (00, 01, 10, 11), so we could use that to represent the values 0 to 3 or perhaps -1 to 2, depending on our requirements. The same exact principle applies to 32 bit integers.

But since they have 32 bits we can store much larger numbers. Starting from 0 we can store a maximum value of 4,294,967,296 inside of an unsigned 32 bit integer. When an integer starts from zero we call it an "unsigned integer", but if we want to store negative numbers then we need a "signed integer", at the expense of halving our range of positive values. To say an integer is "signed" means that we want the + or - sign to be stored in the first bit.

If the first bit is 0 then it's positive, if it's 1 then the number is negative. This is typically something the programming doesn't need to worry about unless they are doing bit-level operations on a variable. You can happily add an unsigned integer with a signed integer, the signed integer will just be converted into an unsigned integer before the addition, although your C++ compiler may give you a warning about it, and for a good reason.

If your unsigned integer contains a value outside of the range that a signed integer can hold, you will get very strange results because the conversion will change the value of the unsigned integer. This can even happen if your integer is already holding the maximum value it can hold and then you try to increase it even more. The value of the integer will "wrap around" back to the very lowest value it can hold which can cause all sorts of issues.

Hopefully you can now see why it's so important to understand what your variables really are and why it isn't a bad thing that C++ requires strongly defined variable types (with an exception I will get to). It doesn't usually pose much of a problem in JavaScript because all numbers are double precision floats, and they can hold a very large range of values, including integer values since an integer can be stored as a float without any fractional value.

A floating point number is really still made from bits just like integers, however the bits a formatted to represent decimal values. The way floats are stored in binary is fairly complicated but isn't really important when it comes to using floats unless you want to do bit-level manipulation on floats, which is rarely necessary. Most programming languages only allow you to manipulate the bits of an integer since it's often useful and very fast for the CPU to perform.

We call these "bitwise operations" and almost every programming language has the same set of bitwise operators. Since all numbers are floating point numbers in JavaScript, if you perform a bitwise operation the variables involved will first be converted into an integer (which allows the ~~ floor trick to work). If you really needed to manipulate the bits of a float you would have to use pointer trickery to treat it as an integer, so I don't think it's possible in a language without pointers such as JavaScript.
edit on 23/4/2021 by ChaoticOrder because: (no reason given)



posted on Apr, 23 2021 @ 06:34 AM
link   
In any case that's way beyond the scope of this guide. Here's some JavaScript code demonstrating the use of common bitwise operators:

var myVar1 = 123 & 321; // the AND operator
var myVar2 = 123 | 321; // the OR operator
var myVar3 = 123 ^ 321; // the XOR operator
var myVar4 = ~123; // the NOT operator
var myVar5 = 123 << 1; // Left shift operator
var myVar6 = 1 >> 123; // Right shift operator

And the equivalent C++ code:

int myVar1 = 123 & 321; // the AND operator
unsigned int myVar2 = 123 | 321; // the OR operator
int32_t myVar3 = 123 ^ 321; // the XOR operator
uint32_t myVar4 = ~123; // the NOT operator
int64_t myVar5 = 123 << 1; // Left shift operator
long myVar6 = 1 >> 123; // Right shift operator


Keep in mind that these operators act on the bits which represent these numbers, not the numbers themselves. So to understand what is really happening you need to know what 123 and 321 would be in binary. I cannot be bothered checking, but rest assured it's a bunch of 1's and 0's. When using the | operator, for every bit in 123, we compare it against the bits in 321. So we compare the first bit of 123 with the first bit of 321, if one or the other bit is 1 then we set the first bit of our result to one, else we set it to 0. We do this comparison for each bit in both integers (so they should have the same number of bits) and can store the result in another integer.

The ^ operator returns 1 if only one or the other bit is 1, this is known as the "exclusive or" operator. The & operator does a similar thing, but only if both bits are 1 will the result be one. The bitwise not operator ~ will simply reverse/flip the bits of an integer, so we only need a single integer variable to perform this operation. The bit-shift operators will shift all of the bits to the left or right. I might go into more detail into bitwise operators in a future guide but you wont really need them when starting out as a programmer, I just think it's important to understand them at an early stage in your learning because it helps to understand how bits work.

Don't be intimidated by the C++ code above, I have purposely made it more complicated than necessary in order to explain a few important points about variables. I should mention, anything after the // is a comment, which means it is text placed along side the code by the programmer to explain what the code is doing, but isn't actually part of the functional code and is completely ignored by the compiler. In many programming languages we signify the end of a line with the ; character, although this isn't strictly required in JavaScript, I still prefer to do it because it makes code easier to read and just looks cleaner imo, although some people may disagree with me on that.

So as we can see both JavaScript and C++ have the same comment style and a similar method of declaring variables, the only real difference is we need to specify the type with C++ instead of just writing var like we do in JavaScript. In the C++ example I use many different types of integers so I could explain the differences between each. The first, which is simply an "int", is a signed 32 bit integer, which means it can hold positive and negative numbers. The "unsigned int" type kind of explains its self, it can hold only positive numbers. The "int32_t" type is exactly the same as writing "int" and writing "uint32_t" is the same as "unsigned int".

int64_t and long mean a signed 64 bit integer. You might be wondering why on Earth would you ever bother writing int32_t instead of just int, and the simple answer is code clarity. More importantly, you know precisely what type of integer it is and how many bits it has. It's not instantly clear how many bits an "int" or "long" should have. Not all compilers are the same, some times a "long" is exactly the same as an "int", but other compilers may treat it as a 64 bit integer (it often depends on if you are using a 32 bit or 64 bit architecture). So I tend to avoid using "int" or "long" completely, and stick to using only int32_t, uint32_t, int64_t, and uint64_t.

That way the compiler knows exactly how many bits you want the integer to have and there is no confusion or potential for the compiler to do things you didn't intend. At this point you may be thinking "but you didn't really explain a string or char or boolean", but actually I have explained them. A char, which is short for character, is really an 8 bit integer. Writing "char" is the same thing as writing "int8_t", they are both a signed 8 bit integer. To create an unsigned 8 bit integer you can write "unsigned char" or "uint8_t". An unsigned char can hold a value from 0 to 255, which can be mapped to ASCII characters in order to represent text characters.

A string is simply a series of chars, or what we call an array. In other words, it's a string of characters which form text, hence we call it a string. There are several ways to create a string in C++, you could create a char array or use the standard library string which is easier to use and more flexible (e.g. you can easily combine strings with the + operator). A boolean is the most simple type of all, it contains only a single bit which can be set to either 0 or 1, which usually corresponds to a true or false value. In most programming languages false is represented by 0 and 1 represents a true value, although anything above 0 will also evaluate to a true boolean value.

For example this if-else statement will always be true because 5 is greater than 0, the C++ code looks and behaves exactly the same:

if (5) {
// do something if true
} else {
// do something if false
}


In the next part of this guide we'll look closer at if-else statements, loops, functions, and other related concepts. For now we'll finish up with how to create a string and boolean variable in JavaScript:

var myString = "Some text";
var myBool = true; // same as myBool = 1

And in C++ you could do this:

std::string myString = "Some text";
bool myBool = false; // same as myBool = 0


Earlier I said that C++ requires you to define the variable type, but that isn't really true. Modern versions of C++ allow you to use "auto" much like you use "var" in JavaScript. So instead of writing std::string (the std:: part means we are using the standard library, but don't worry about that for now) we can just write "auto" and it will automatically recognize that we want to create a string. I have emphasized the importance of explicitly defining variable types, however the auto keyword can be useful in situations where your variable types are very long and it can make your code look a lot cleaner, but shouldn't be used in places where it could cause confusion.

Hopefully I have shown that C++ isn't the scary language it's often said to be. Yes it has things like pointers which may be hard to grasp at first but the power they give you is immense. The syntax rules are strict but in a way it forces you to learn a better way of programming and those lessons extend across to every other programming language. I often have more trouble reading JavaScript just because the syntax is so flexible and it only has six variable types, and it may not be clear what is being used. Your control over the variable types is very limited which can be an issue especially when you need to do low level bit manipulation and memory management.
edit on 23/4/2021 by ChaoticOrder because: (no reason given)



posted on Apr, 23 2021 @ 07:33 AM
link   
10 A$ = "Holy Moly, how far we've come."
20 PRINT A$
30 GOTO 10

cheers, I'll read this properly, as a self taught coder from the old days. always good to keep the brain lubricated.



posted on Apr, 23 2021 @ 08:21 AM
link   
a reply to: ChaoticOrder

S&F reminds me of all the headaches I endured when I was first learning. And I agree. A lot of people I've met just speed through it so they can write some ass code and ignore the fundamentals. They don't really understand what they are doing or how it works. They've just memorized enough to get the job done, but if anything goes wrong they are lost most of the time and have to call someone more experienced to sort it out for them.

I've been that person called in just to stare at a mass of fecal matter. There's been a few times where I just told the boss that they should just let me start from scratch and do it properly.



posted on Apr, 23 2021 @ 09:18 AM
link   
try assembler

if you think c++ in complicated



posted on Apr, 23 2021 @ 09:37 AM
link   

originally posted by: AutomateThis1
a reply to: ChaoticOrder

I've been that person called in just to stare at a mass of fecal matter. There's been a few times where I just told the boss that they should just let me start from scratch and do it properly.


We just had a guy leave and I'm sitting at this point at this very moment. Some times it's better just to start over and write it correctly instead of playing whack-a-mole with buggy ass code lol



posted on Apr, 23 2021 @ 09:46 AM
link   

originally posted by: acackohfcc
try assembler

if you think c++ in complicated


I started to learn how it all worked using direct HEX back in the z80 days. Three registers only. Just looking at the machine filled it's ram. Oh those were the days.

Assembler..
You kids with your fancy tools !!




posted on Apr, 23 2021 @ 09:54 AM
link   

originally posted by: Tekner

originally posted by: AutomateThis1
a reply to: ChaoticOrder

I've been that person called in just to stare at a mass of fecal matter. There's been a few times where I just told the boss that they should just let me start from scratch and do it properly.


We just had a guy leave and I'm sitting at this point at this very moment. Some times it's better just to start over and write it correctly instead of playing whack-a-mole with buggy ass code lol


I remember near well over 20 years ago, rocking up to a programming class at a technical college, late...

I sit down, and this kid runs up to me asking to see my code. I show him, and he's asking how I did this and that.

The lecturer stops talking, stares at me, and says "If you cannot get to class on time, do not pester the other students asking them how to do things."

the kid sat down, I just stared in a sort of disgruntled manner... Turns out most of the class had coded, in what the lecturer called, Spaghetti code, cobbling everyone else's code together. He didn't say a thing to me as I passed.

funny how life is, when you look like a bum, but can do things the 'nerds' can't...

hated that time in life too, I might add.



posted on Apr, 23 2021 @ 11:46 AM
link   

originally posted by: jerich0
10 A$ = "Holy Moly, how far we've come."
20 PRINT A$
30 GOTO 10

cheers, I'll read this properly, as a self taught coder from the old days. always good to keep the brain lubricated.



Looks like something from a Apple ][ or maybe a TRaSh-80....
edit on 23-4-2021 by ByteChanger because: (no reason given)

edit on 23-4-2021 by ByteChanger because: (no reason given)



posted on Apr, 23 2021 @ 12:52 PM
link   
I disagree, I’ve worked with many a lowlevel dev earning peanuts , the big bucks is the ability to fix bugs, regression testing, retro engineer , automation, not just go from scratch. Taking 12000 lines of code in 20 different modules across 10 different platforms... if u can’t debug stick to var=my at+10; var2=“Look what I did in school today”

I don’t think this is the right forum to regurgitate beginner learning ( whatever the subject) as some sort of informative thread! ,otherwise I’ll start one next with basic multiplication

a reply to: AutomateThis1



posted on Apr, 23 2021 @ 12:53 PM
link   
a reply to: ChaoticOrder

Great work, ChaoticOrder!

I've been involved in IT at work since the 90s, but I've never gone past the first few hurdles to learning a programming language. Too many colleagues clouded my choices, plus other 'dirty' IT work just kept me too busy. Besides, I'm on the wrong side, age-wise, of my career for it to matter for work, but I'd love to get a head start to add to my future retirement hobbies!

Also, I was always intimidated by numbers, but many said that to be a programmer you first need to understand english. Your tutorial is very well written and you explain brilliantly complex concepts.

If I can keep up, I may convert your posts to PDFs for easier reference once you do a few more... if I do I'll happily make them available here if you don't object.

Finally, just so I can gauge what we can expect, how deep do you think you'll take us before letting go of our hands to fend for ourselves?

Thanks again for the time and effort!




posted on Apr, 23 2021 @ 01:07 PM
link   
a reply to: Midnite247

Uh... ok? I never said I couldn't do any of that, but do you feel better now that you got that out?

If the program is generally good to go, and there's only a bit here and there then of course I'm going to just fix the errors.

That's not what I was talking about. If that's what you took away from my comment about staring at a #ty piece of work and scrapping it because it's complete garbage then you may want to worry more about your English skills than your ability to do "basic multiplication."



posted on Apr, 23 2021 @ 01:43 PM
link   
No, I wasn’t worried about that, not directed at you, just not sure basic education is worth a thread, regardless of the subject, just me, so I get the response That I would give “yeah, well don’t involve then” lol
So yeah, there’s that.
Meh- sometimes u read something and can’t be bothered , but say out loud when u should prob just turn the page!

a reply to: AutomateThis1




posted on Apr, 23 2021 @ 02:10 PM
link   

originally posted by: ChaoticOrder
Essentially all programming languages have a very similar set of variable types, which usually include integers, floats, chars, strings, and booleans, which are the 5 main types I will focus on in this first part of my programming guide.

Things get more confusing with Assembly.



PS: great idea for a thread.



posted on Apr, 23 2021 @ 02:22 PM
link   
So just to clarify, it’s not a great idea for a thread, 😂😂
So my job is to cut to the chase, separate the wheat from the chaff,
in that when ur talking careers ur already talking full stack , back end/front end developer , to talk about basic entry level coding, is like starting a financial thread of economics starting with basic maths.
The point being anyone interested in the thread already has a grasp of the basics, and an explanation of the basics is of no interest to anyone who has no interest in the topic.
Unless the thread is going to progress to a intermediate/advanced understanding of coding, there’s no point in it.
Just sayin
a reply to: ArMaP



posted on Apr, 23 2021 @ 02:43 PM
link   
a reply to: Midnite247

Apart from it being easy to find information, my main problem with this thread is that it's actually pretty offputting for beginners and if they were already intimidated by coding, this is going to make it worse.

It doesn't really explain anything other than how c++ represents some types and for some reason bitwise operators without even explaining what a statement is, why you need to know these things or even what programming is.

Dumping everything about signed and unsigned integers, the difference between floats and doubles and a whole bunch of other things that aren't actually helpful for someone who's a complete beginner.

Yes those things are important, but getting all that information at once in a big dump like that is too much. Especially before you even understand what programming even is.

There's at least 5 different concepts being discussed but only at a relatively shallow level that someone without any basic understanding is going to be lost quickly.

It's not bad information, i just don't think it's presented well as an 'Intro to programming'. It's too advanced for complete beginners, but isn't really helpful to anyone who's not.



posted on Apr, 23 2021 @ 02:47 PM
link   
Sactly
a reply to: dug88




posted on Apr, 23 2021 @ 03:38 PM
link   
Who cares? People are free to create a thread about almost anything. If ChaoticOrder wants to do it, and it's not against T&C, then there's nothing there to prevent them from doing it.

Maybe instead of saying it's useless or pointless or whatever offer some constructive criticism about how to present in a friendly manner.

The information presented in PART 1 wasn't even that technical. I don't see how it would scare anyone off. If someone is interested or curious they're going to be interested, if not then they're just going to gloss over it.
edit on 2342021 by AutomateThis1 because: (no reason given)



posted on Apr, 23 2021 @ 03:43 PM
link   
a reply to: ChaoticOrder

Excellent!

Okay here is my issue, I registered on stack overflow but I didn't dare to formulate the question yet, there. I just recently started to go deep into programming custom modes on engine management systems.

The code some of them use are very like C#, is what I focused on. But anyways here is the thing:

Electronic throttle valve and digital potentiometer with jitter removed. I measure the voltage and calculate a baseline value over time for different smart adjustments.

For that, I checked, the variable type the ECUs compiler use is double, beyond that I have no idea. The problem is, I measure this over time but inside the logs that I can only activate for so long, there are sporadic peaks that can not physically happen that fast. They have to happen inside the software. I measure the time delay the spring brings into when the throttle isn't pressed and all that. It's the software and the algorithm I use runs on other stuff without derivation, I checked with one of those arduinos, the log on the SD card never had these peaks.

Could there be some overflow or decimal shifting happening? I did rounding too, and removed it, the only operation I can not look into. Issue stayed.


originally posted by: ThatDamnDuckAgain


Code's like this

double BaselineThrottleVal = 0; //real time Throttle value
double BaselineThrottleValAvrg = 0; //averaged Throttle value
double CollectiveBaselineThrottleVal =0; //Temp var for collecting measurements and do comparision
int CollectiveBaselineThrottleValCounter =5;


loop
...
...
secret stuff happening not even touching this var
...
...


//comment: this for loop is constantly running on a different thread, all custom function are on own thread but thread safe and can touch global vars

for(int i=0;i<CollectiveBaselineThrottleValCounter ;i++)
{
BaselineThrottleVal = IO.Thrttle; //IO.Thrttle contains double 0.0-0.999 representing 0-100%, it's filled by ECU on runtime.
CollectiveBaselineThrottleValCounter++;
CollectiveBaselineThrottleVal += BaselineThrottleVal;

//comment: (double) casts int into double
}
BaselineThrottleValAvrg = CollectiveBaselineThrottleVal / (double)CollectiveBaselineThrottleValCounter ;

loop end



Sometimes there's values like 391.13130310 just as an example.

Note this is not a feedback loop with the throttle, I can only do other things like inject less or more fuel, shift injection timing and duration or ignition timings and angles or open exhaust and intake gates (few other stuff). It doesn't have any effect on the main ECUs safety mechanisms, yes it's about programming a car


Hope I didn't confuse too much, like I say, very beginner.
edit on 23.4.2021 by ThatDamnDuckAgain because: (no reason given)



posted on Apr, 23 2021 @ 03:48 PM
link   
Thanks to ArMap the code is now visible


Add: I use similar code for detecting the load cell value rise when the shifter is pulled to disable all the anti knock safety because the gear change is detected as knocking. No problems there, logged it too.

edit on 23.4.2021 by ThatDamnDuckAgain because: Thanks to ArMap the code is now visible




top topics



 
13
<<   2 >>

log in

join