C++ motivational thread

double absolute_value(double number)
{
    if(number < 0) number *= -1;
    return number;
}

Took me a minute. Human Resources Machine didn’t have such fancy operators. Ha ha. This will get me by for now.

Yes, it’s in the C(++) lib too:

https://en.cppreference.com/w/cpp/numeric/math/fabs

Be careful with floating point stuff since not making operands doubles occasionally make you cast to an integer. The rule is if one component of an arithmetic expression is a double then the result is a double, but we can slip up with, e.g. double half = 1 / 2;:

I just found out that I shouldn’t name my function abs().

The book heavily warns about type conversion issues, so I’m well covered there (or so I think).

Btw, abs() does essentially the same as a rectifier in electronics. For example: abs(sin(x)) will give all positive cycles. Then smooth it out with some averaging function (like a capacitor in electronics) to get a straight line for steady DC (AC to DC conversion).

It would be neat as hell to put together a basic electronics simulator. Hopefully, in time. And open source of course.

I cut out from work early today, back on the c++ book. It was 104F yesterday, and supposed to get 102F today. And we don’t have what we need at work to get things done today, so good day to cut out.

Last I left off I was poking things into some get-by-for-now diy functions, so as to stay within the limitations of what has been covered in the book. Absolute value, min, max, odd even, equality, length unit check, convert to meters, sum of input lengths, list of input lengths, sorting. It seemed silly at first to write extra code for functions, but it does make things much easier to read.

Seems like I have been camping out in this chapter for a minute. It might actually take longer this time around to get back to the graphics chapters. That’s ok though. I feel like I’m having a better go it this time.

Rather than doing exercises today, I’m working out some things that seem useful.

Toward a string number detector and string to double converter. main() is only for demonstration that the functions work.

#include "std_lib_facilities.h"
// Checks that input is a numeric character and converts to an int

bool number_detect(char input)
{
	int character_code = input;
	bool is_number = true;
	if(character_code < 48 or character_code > 57)		// If any non-numeric character is detected
		is_number = false;
	
    return is_number;
}

int number_char_to_int(char char_digit)
{
	int int_digit;
	for(int i = 0; i < 10; ++i){						// 10 is digits 0-9
		int char_code = char_digit;
		if(char_code == i+48){							// 48 is '0' character code
			int_digit = i;
			break;
		}
	}

	return int_digit;
}

int main()
{
	while(true){
		cout << "Enter a numeric character: ";
		char input;
		cin >> input;
		if(number_detect(input) == true)
			cout << number_char_to_int(input) << " is now converted to an int.\n";
		else
			cout << input << " is not a numeric character.\n";
	}
}

Yes, that’s a good exercise for getting specific, reusable functions. Remember that modern compilers should inline a lot of this stuff automagically, and you can put the inline keyword in front of your functions to beg the compiler to do it (but it doesn’t have to), so putting something in a function doesn’t mean it’s always treated like a function (new stack frame etc).

The first function can be simplified and improved btw:

bool is_char_number(char c) 
{ 
    return c >= '0' and c <= '9'; 
}
  • It’s a bool, so function name is more obvious question.
  • Single letter variables are fine in tiny things where they are used immediately and what they are is obvious and input was very generic anyhoo
  • Using the characters themselves removed the need for a comment.

In the second function if it’s not a char code, you are:

  • returning the unitialized int_digit which will be zero in most compilers (but could be residual garbage too)
    – maybe initialize it to -1 so you have a built-in error code being returned on failure
    – or do an assert at the beginning of the function to ensure the char is in range.
  • Rather than a loop, you could:
const int ascii_digit_start_pos = 48;
int char_val = char_digit - ascii_digit_start_pos; // using implicit cast to int
if (char_val >= 0 and char_val <= 9)
    return char_val;
else
    return -1;
1 Like

Good stuff there to soak up Snookoda. Thanks for taking the time.

No problemo, there are almost always multiple solutions to problems and multiple ways to do things. I sometimes mess around with competitive programming stuff and the solutions the pros come up with are mind boggling at times but it’s great to see what I didn’t.

I have barely peeked at some code from programmers who are trying to make things as small and efficient as possible, and it looked like greek to me. I imagine some of the competitive stuff gets pretty funky to decipher.

No programming today. Feeling too tired all day so far. Same yesterday.

It’s good to take a break for a few days every now and then anyways, to let your brain do some sorting and filing.

A couple of days of aspirin seems to be helping. Hopefully things keep improving. If not, I’ll have to visit a doc. Sleeping a lot and catching up on some tech news inbetween. I still really want to see Haiku OS take off, but it looks like it is frozen in time. It’s just a desktop os, nothing more. Not an os for a workstation, server, portable device, internet of things, etc. It doesn’t have all the fragmentation, baggage, and ancient heiroglyphics of the linux world. A single unified api (reportedly very clean). But still no gpu acceleration, and still a lack of applications.

It sounds good, I’ve heard about it and have heard great things about scheduling/threading in BeOS but never looked further. This could be a positive rabbit hole to sniff around in! :slight_smile:

<one video later>

Looks like it’s getting there, all the usual suspect packages are available too. Very dated UI, they could do with rounded windows, better fonts and general theming support from the start. I see it’s been mentioned for a few years and that better themes exist:

I would be interested in hearing your impressions if you look around further. There seems to be good potential there, on the surface at least. Yes, the interface is dated for sure. I still prefer visually functional over disfunctionally modern though, such as the too bare flat interface here of Discourse.

Oh I’m not too big on visuals either, I just prefer clean lines if given a choice and 90s and noughties weren’t clean imo. There seems to be an assumption that going from indented/shadowed to flat is a cycle that will eventually return to the 90s because of some unknown natural law, but looking at both with no nostalgia there’s a clear winner. We haven’t got to futuristic wireframe/flat yet and 90s style isn’t on that course!

I see Haiku allows loading 3rd party drivers without having a kernel compiled with “module” loading enabled, so that’s a big plus point. I love this quote from the guy who did the RISC-V port:

Linux looks like advanced DOS or node.js packages that can run on bare metal. Haiku looks like real OS with consistent design.

Greetings, from a Haiku VM! I thought Firefox was available here, but can’t find it in “HaikuDepot” package manager so I’m in their browser, WebPositive.

I’m going to have a sniff around for a while, see what I can find. LibreOffice installed okay, it has g++ and make pre-installed but no xmake or cmake ports available by the looks of things yet. Got vim too so good to go for a while!

I have to say that installation was very fast btw.

Nice. It has been a good minute since I ran Haiku (also in a vm). I’m guessing that not too much has changed since though. Definitely keep me updated on your impressions. Maybe I’m way off, but it seems to have good potential as a least bs desktop os. Although it does have some big issues, such as lack of gpu acceleration and lack of software that is available for other os’s. I feel like applications will have to come before there is enough interest for hardware acceleration to happen. But that seems a tough pill for developers to swallow, with so few users and no guarantees of anything changing. And it might make for good stomping grounds for someone such as myself (programming noobs) to begin learning an api (eventually).

I spoke too soon about cmake, I found it searching on the command line with pkgman. It was also in the HaikuDepot, but there are “Featured packages” and “All packages” tabs which defaults to Featured and I didn’t notice that.

I need to find something that brings modern autocomplete to this world.

I never got so far as needing make files, so I’m no help there. I went ahead and reinstalled Haiku to have a refresher look. I still wonder why relatively so few people seem to have taken interest in it, even if only as a hobby system to tinker with.

I thought I had replied before, but I guess I forgot to click the reply button.