Jump to content

My Fuzzy Boolean Implementation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Hmm, well I read through WingedPanthers stuff and it seemed relatively interesting and it was something that I'd never coded before. I also needed a reason to freshen up on my C++ skills as I've pretty much been coding in C for the past few months.

So, I coded an implementation and the differences should be fairly obvious when you see the code.

Hm, I was a bit rusty with C++ skills so there could be some bugs that I missed, but I think I cleaned most of the **** up.

You should be able to tell how to use the class from the examples in main.

(The Ret_Val() is there for the sake of convenience only)

If you notice any crap lines or bugs just say something.


// Fuzzy Boolean Implemenation

#include <iostream>

#include <time.h>

using namespace std;


// In comparisons, an F_Bool type must come first in order for a fuzzy boolean comparison to occur

// Also, we use 0 to 100 as opposed to 0.0 to 1.0

// All types are infered as a decimal (1 to 100) except.. bool

// Seperate handler functions are required for f_bool and bool, everything else is handled by a generic


class F_Bool 

{

	public:

		template <typename Type> F_Bool(Type data);

		inline int Ret_Val() {return this->percent;}

		

		int operator&&(F_Bool val);

		int operator||(F_Bool val);

		

		int operator&&(bool val);

		int operator||(bool val);

		

		template <typename V_Type> int operator&&(V_Type val);

		template <typename V_Type> int operator||(V_Type val);

		

		int operator!(void) {return (100 - this->percent);}

		

		bool To_Bool();

	private:

		unsigned int percent;

		string out_range; 

		int max_rand;

	

		inline int Get_Rand() {return (rand() % this->max_rand);}

	

};


F_Bool f_true(100);

F_Bool f_false(0);


int main(int argc, char * argv[])

{

	double number = 51;

	F_Bool f_b((float)56);

	

	int number2 = 12;

	F_Bool f_b2((unsigned char)number2);

	

	cout << f_b.To_Bool() << endl;

	cout << (f_b && f_b2) << "\t" << endl;

	cout << !(f_b2);

	

	return 0;

}


template <typename Type> F_Bool::F_Bool(Type data)

{

	this->max_rand = 101; // We want from 0 to 100

	this->out_range = "Percent chance is out of range!\n";

	srand(time(NULL));

	try {

		if (data > 100 || data < 0) throw this->out_range;

		this->percent = static_cast<unsigned int>(data);

	}

	catch (string err) {

		cout << err << endl;

	}

}


// Next two are for F_Bools

int F_Bool::operator&&(F_Bool val)

{

	if (this->percent > val.percent) return val.percent;

	else return this->percent;

}		


int F_Bool::operator||(F_Bool val)

{

	if (this->percent > val.percent) return this->percent;

	else return val.percent;

}


// Next two are for bools

int F_Bool::operator&&(bool val)

{

	if (val) return this->percent;

	else val;

}		


int F_Bool::operator||(bool val)

{

	if (val) return 100;

	else return this->percent;

}


// Next two are for everything else

template <typename V_Type> int F_Bool::operator&&(V_Type val)

{

	try {

		if (val > 100 || val < 0) throw this->out_range;

		if (this->percent > val) return static_cast<int>(val);

		else return this->percent;

	}

	catch (string err) {

		cout << err << endl;

		return 0;

	}

}		


template <typename V_Type> int F_Bool::operator||(V_Type val)

{

	try {

		if (val > 100 || val < 0) throw this->out_range;

		if (this->percent > val) return this->percent;

		else return static_cast<int>(val);

	}

	catch (string err) {

		cout << err << endl;

		return 0;

	}

}


// Convert the F_Bool to a bool

bool F_Bool::To_Bool() {

	if (Get_Rand() <= this->percent) return true;

	else return false;

}




#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Is there a reason why you are having the &&, ||, and ! operators return ints instead of F_Bools? It seems counter-intuitive to me. I'm guessing you used ints over floats because of how the random number generator works in C++.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
There is no reason to return a F_Bool. F_Bool is like a container for the calculations I guess you could say.

That is just like a bool is normally just going to be a 4 byte integer (x86 Intel).

Returning the integer takes nothing away from how it operates, it does however make it cleaner.

#4
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Hmm I can't edit my post for obvious reasons.

But I would also like to say that it seems bools are commonly implemented as one byte.

#5
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
And because floats can't represent 0.10 for example exactly.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Frequently, probabilities are not integer percentages. 5.5%, for example.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
In that case extending my version to allow for doubles and still using my 0.0 to 100.0 system will allow for better accuracy than your implementation.

Also just to make this more clear, here is a list of acceptable use..

f_bool_var && true
f_bool_var && false
f_bool_var && (some_type)45
f_bool_var && f_bool_var2

(Then all four of those using ||)

!f_bool_var

And then any variation of that.

Also remember that floats, doubles, integers, etc all will work when initializing an F_Bool type.

A nice bit of versatility.