Thread overview
Eşleme tablosu ".require" işlem sırasına göre davranış değiştirmesi
Jul 25, 2019
kerdemdemir
Jul 25, 2019
kerdemdemir
Aug 27, 2019
kerdemdemir
July 25, 2019

Uzun kod için özür dilerim .

İşin garip kısmı eğer önce yazdırırsam :

writeln(temp.scoreListMissedChance);
double currentMissChance = temp.GetCurrentMissedScore();

Sonuç beklediğim gibi

[HashableDouble(0.965):1, HashableDouble(0.945):1, HashableDouble(0.935):1, HashableDouble(0.95):1, HashableDouble(0.94):1, HashableDouble(0.93):1, HashableDouble(0.925):1, HashableDouble(0.92):1, HashableDouble(0.96):1, HashableDouble(0.955):1]

Fakat önce GetCurrentMissedScore cağırıp sonra yazdırırsam :

double currentMissChance = temp.GetCurrentMissedScore();
writeln(temp.scoreListMissedChance);

[HashableDouble(0.965):1, HashableDouble(0.945):1, HashableDouble(0.95):0, HashableDouble(0.935):1, HashableDouble(0.95):1, HashableDouble(0.94):1, HashableDouble(0.93):1, HashableDouble(0.925):1, HashableDouble(0.92):1, HashableDouble(0.96):1, HashableDouble(0.955):1]

yeni bir HashableDouble(0.95) elemanı ekleniyor :/ .

Yardımcı olabilirmisiniz ? Eğer 0.95 gibi bir girdi varsa bir daha girilmesini istemiyorum.

import std.stdio;
import std.algorithm;
import std.string;
import std.typecons;
import std.array;
import std.math;

struct HashableDouble
{
   int opCmp( double rhs ) const {
       auto diff = curVal - rhs;
       if (  fabs(diff) < 0.001 )
           return 0;
       else if ( diff < 0 )
           return -1;
       else
           return 1;
   }

   double curVal;
}


struct AdjustableVal ( T = double )
{
	this ( string name, T initVal, T maxVal, T minVal, T step, bool goUpForSafety )
	{
		this.name = name;
		this.initVal = initVal;
		this.curVal = initVal;
		this.maxVal = maxVal;
		this.minVal = minVal;
		this.step = step;
		this.goUpForSafety = goUpForSafety;
	}

	double GetScore( HashableDouble level )
	{
           return scoreList.require(level, 0.0);
	}

	double GetCurrentScore()
	{
           return scoreList.require(HashableDouble(curVal), 0.0);
	}

       double GetCurrentMissedScore()
	{
           return scoreListMissedChance.require(HashableDouble(curVal), 0.0);
	}

       int opCmp(T s) {
           comparedValue = s;
           return s < curVal;
       }

	T opBinary(string op)(T rhs)
	{
	    return mixin("rhs"~op~"curVal");
	}

	string name;
       T initVal;
	T curVal;
	T comparedValue = 0;
	T maxVal;
	T minVal;
	T step;
	double[HashableDouble] scoreList;
	double[HashableDouble] scoreListMissedChance;
	bool   goUpForSafety;
}

void main()
{
   auto temp = AdjustableVal!double( "curPeakDrop", 0.95, 0.97, 0.92, 0.005, false);

   for ( auto i = temp.minVal; i < temp.maxVal; i += temp.step )
   {
       temp.curVal = i;
       temp.scoreListMissedChance[HashableDouble(i)] += (1) ;
   }
**    writeln(temp.scoreListMissedChance);
   double currentMissChance = temp.GetCurrentMissedScore();**

}

Erdemdem

--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]

July 25, 2019

Birkaç kere okumuştum 7-8 sene önce gerçekten güzel bir makale idi o .

Fakat burda benim özel opCmp 'em sayesinde çalışmasını bekliyordum:

struct HashableDouble
{
   int opCmp( double rhs ) const {
       auto diff = curVal - rhs;
       if (  fabs(diff) < 0.001 )
           return 0;
       else if ( diff < 0 )
           return -1;
       else
           return 1;
   }

   double curVal;
}

Fakat özelleştirmem gereken

   bool opEquals(ref const HashableDouble rhs) const
   {
       return fabs(rhs.curVal - this.curVal) < 0.001;
   }

Fonksiyonu imiş gözden kaçırmışım.

Bu arada bir önceki hatalı kodda writeln'in sırasına göre davranışın değişmesini hala çok ilgincime gidiyor.

Erdemdem

--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]

July 25, 2019

O değerlerin hiçbirisi 0.95 olamaz çünkü 0.95 kesirli sayı türleriyle ifade edilemez:

   double currentMissChance = temp.GetCurrentMissedScore();
   writeln(temp.scoreListMissedChance.byKeyValue
           .map!(t => format!"%.20g:%.20g"(t.key.curVal, t.value)));

'
["0.94000000000000005773:1", "0.9600000000000000755:1", "0.92500000000000004441:1", "0.93500000000000005329:1", "0.95000000000000006661:1", "0.94500000000000006217:1", "0.93000000000000004885:1", "0.95500000000000007105:1", "0.96500000000000007994:1", "0.92000000000000003997:1"]
'
:)

"What Every Computer Scientist Should Know About Floating-Point Arithmetic" gibi yazıları okuyabilirsin.

Ali

--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]

August 27, 2019

Benim işimi gören yapı şu oldu paylaşmak istedim

struct HashableDouble
{
enum EPSILON = 0.00001;

double curVal;
size_t toHash() const nothrow @safe
{
return cast(size_t)round(this.curVal * (1.0 / EPSILON));
}

bool opEquals(ref const HashableDouble rhs) const
{
return (rhs.curVal - this.curVal) < EPSILON;
}
}

--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]