GTEST_TEST(Fixed, version) { Fixed version(1, 0); EXPECT_EQ(version.toString(), "1.0"); Fixed fixed = 3.7; EXPECT_EQ(fixed.toString(), "3.7"); }
int IntersectRaySphereX( const SphereX& sphere, const Vector3X& p, const Vector3X& dir, Fixed* t ) { Vector3X raySphere = sphere.origin - p; Fixed raySphereLen2 = DotProduct( raySphere, raySphere ); Fixed sphereR2 = sphere.radius*sphere.radius; if (raySphereLen2 < sphereR2) { // Origin is inside the sphere. return grinliz::INSIDE; } else { // Clever idea: what is the rays closest approach to the sphere? // see: http://www.devmaster.net/wiki/Ray-sphere_intersection Fixed closest = DotProduct(raySphere, dir); if (closest < 0) { // Then we are pointing away from the sphere (and we know we aren't inside.) return grinliz::REJECT; } Fixed halfCordLen = (sphereR2 - raySphereLen2) / DotProduct(dir, dir) + (closest*closest); if ( halfCordLen > 0 ) { *t = closest - halfCordLen.Sqrt(); return grinliz::INTERSECT; } } return grinliz::REJECT; }
Fixed Fixed::operator--(int) { Fixed res; res.setRawBits(getRawBits()); --_raw; return (res); }
Fixed Fixed::operator++(int) { Fixed res; res.setRawBits(getRawBits()); ++_raw; return (res); }
AVisFixed::AVisFixed(const string& aName, Elem* aMan, MEnv* aEnv): AVisWidget(aName, aMan, aEnv) { SetEType(Type(), AVisWidget::PEType()); SetParent(Type()); Fixed* fx = new Fixed(); fx->set_reallocate_redraws(true); iWidget = fx; iWidget->show(); }
Fixed Fixed::operator*(Fixed const &rhs) { Fixed f; int result; result = this->_fixe * rhs.getRawBits(); result += 1 << (this->_fractionalbits - 1); result >>= this->_fractionalbits; f.setRawBits(result); return (f); }
void AVisDrawing::OnUpdated_Y(int aY) { Allocation alc = iWidget->get_allocation(); Container* parent = iWidget->get_parent(); // Y change handles only by Fixed parent Fixed* fx = dynamic_cast<Fixed*>(parent); if (fx != NULL) { fx->move(*iWidget, iX, iY); } }
Fixed Fixed::operator/(Fixed const &rhs) { Fixed f; int result; result = this->_fixe << _fractionalbits; result += this->_fixe / 2; result /= this->_fixe; f.setRawBits(result); return (f); }
void BudgetWindow::RefreshBudgetGrid(void) { fIncomeGrid.MakeEmpty(); fSpendingGrid.MakeEmpty(); CppSQLite3Query query = gDatabase.DBQuery("select category,amount,period from " "budgetlist order by category", "BudgetWindow::RefreshCategories"); while(!query.eof()) { BString cat = DeescapeIllegalCharacters(query.getStringField(0)); Fixed amount; amount.SetPremultiplied(query.getInt64Field(1)); BudgetPeriod period = (BudgetPeriod)query.getIntField(2); ReportGrid *grid = (amount.IsPositive()) ? &fIncomeGrid : &fSpendingGrid; int32 index = grid->CountItems(); grid->AddItem(); grid->SetRowTitle(index, cat.String()); Fixed f(amount.AbsoluteValue()); switch(period) { case BUDGET_QUARTERLY: { f /= 3; Fixed qamt(amount); qamt *= 4; grid->SetValue(12,index,qamt); break; } case BUDGET_ANNUALLY: { f /=12; grid->SetValue(12,index,amount); break; } default: { Fixed mamt(f); mamt *= 12; grid->SetValue(12,index,mamt); break; } } for(int32 i=0; i<12; i++) grid->SetValue(i,index,f); query.nextRow(); } }
int main(void) { Fixed a; Fixed b( a ); Fixed c; c = b; std::cout << a.getRawBits() << std::endl; std::cout << b.getRawBits() << std::endl; std::cout << c.getRawBits() << std::endl; return 0; }
int main() { Fixed n(-10.0); Fixed f(-100.0); Fixed d(2.0); Fixed a(1.5); Fixed b(-1.5); Fixed c = -a; Fixed r = d / (n - f); cout << r.to_float() << ' ' << r.num << endl; cout << a.num << ' ' << b.num << ' ' << c.num << endl; printf("%X %X\n", a.num, b.num); return 0; }
void BudgetWindow::RefreshCategories(void) { fCategoryList->Clear(); fIncomeRow = new BRow(); fCategoryList->AddRow(fIncomeRow); fSpendingRow = new BRow(); fCategoryList->AddRow(fSpendingRow); fIncomeRow->SetField(new BStringField(TRANSLATE("Income")),0); fSpendingRow->SetField(new BStringField(TRANSLATE("Spending")),0); CppSQLite3Query query = gDatabase.DBQuery("select category,amount,period,isexpense from " "budgetlist order by category", "BudgetWindow::RefreshCategories"); float maxwidth=fCategoryList->StringWidth("Category"); while(!query.eof()) { BString cat = DeescapeIllegalCharacters(query.getStringField(0)); Fixed amount; amount.SetPremultiplied(query.getInt64Field(1)); BudgetPeriod period = (BudgetPeriod)query.getIntField(2); BRow *row = new BRow(); if(query.getIntField(3)==0) fCategoryList->AddRow(row,fIncomeRow); else fCategoryList->AddRow(row,fSpendingRow); row->SetField(new BStringField(cat.String()),0); BString amountstr; gDefaultLocale.CurrencyToString(amount.AbsoluteValue(),amountstr); amountstr.Truncate(amountstr.FindFirst(gDefaultLocale.CurrencyDecimal())); amountstr.RemoveFirst(gDefaultLocale.CurrencySymbol()); row->SetField(new BStringField(amountstr.String()),1); float tempwidth = fCategoryList->StringWidth(cat.String()); maxwidth = MAX(tempwidth,maxwidth); row->SetField(new BStringField(BudgetPeriodToString(period).String()),2); query.nextRow(); } fCategoryList->ColumnAt(0)->SetWidth(maxwidth+30); fCategoryList->ExpandOrCollapse(fIncomeRow,true); fCategoryList->ExpandOrCollapse(fSpendingRow,true); }
bool Fixed::operator>(Fixed const &rhs) { if (this->toFloat() > rhs.toFloat()) return true; else return false; }
static std::string fixtos(Fixed f) { // std::cout << __func__ << " (" << __FILE__ << ") - line: " << __LINE__ << std::endl; //debug std::ostringstream ss; ss << f.toFloat(); return ss.str(); }
void DirectoryData::SetData ( Fixed newData ) { m_type = DIRECTORY_TYPE_FIXED; #ifdef FLOAT_NUMERICS m_fixed = newData.DoubleValue(); #elif defined(FIXED64_NUMERICS) m_fixed = newData.m_value; #endif }
void MapVec2::normalize() { Fixed n = x * x + y * y; // Already normalized. if (n == Fixed::ONE) { return; } n = n.sqrt(); // zero. if (n == Fixed::ZERO) { return; } n = Fixed::ONE / n; x *= n; y *= n; }
int main( void ) { Fixed a; Fixed const b( 10 ); Fixed const c( 42.42f ); Fixed const d( b ); a = Fixed( 1234.4321f ); std::cout << "a is " << a << std::endl; std::cout << "b is " << b << std::endl; std::cout << "c is " << c << std::endl; std::cout << "d is " << d << std::endl; std::cout << "a is " << a.toInt() << " as integer" << std::endl; std::cout << "b is " << b.toInt() << " as integer" << std::endl; std::cout << "c is " << c.toInt() << " as integer" << std::endl; std::cout << "d is " << d.toInt() << " as integer" << std::endl; return (0); }
Fixed next(Fixed range) { return Fixed::from_val(next(range.val())); }
inline Fixed lsqrt(Fixed n) { return Fixed::from_val(lsqrt(n.val())); }
float Fixed::operator-(Fixed const &P2) const { return(this->toFloat() - P2.toFloat()); }
void TransactionItem::DrawItem(BView *owner, BRect frame, bool complete) { BString string; Locale locale = fAccount->GetLocale(); BRect r(frame); r.right--; rgb_color linecolor; if(IsSelected()) { linecolor.red=120; linecolor.green=120; linecolor.blue=120; owner->SetHighColor(GetColor(BC_SELECTION_FOCUS)); owner->SetLowColor(GetColor(BC_SELECTION_FOCUS)); owner->FillRect(frame); owner->SetHighColor(linecolor); owner->StrokeRect(frame); owner->SetHighColor(255,255,255); } else { linecolor.red=200; linecolor.green=200; linecolor.blue=200; if(fStatus==TRANS_RECONCILED) { owner->SetHighColor(232,232,232); owner->SetLowColor(232,232,232); owner->FillRect(frame); owner->SetHighColor(linecolor); owner->StrokeLine(r.LeftBottom(),r.RightBottom()); owner->SetHighColor(255,255,255); } else { owner->SetHighColor(255, 255, 255); owner->SetLowColor(255, 255, 255); owner->FillRect(frame); // owner->SetHighColor(222, 222, 222); owner->SetHighColor(linecolor); owner->StrokeLine(r.LeftBottom(),r.RightBottom()); } } owner->SetHighColor(0, 0, 0); BRect cliprect; BRegion clip(cliprect); float xpos = TLeftPadding(); float ypos = r.top + TRowHeight(); // Date cliprect.left = xpos; cliprect.right = xpos + TDateWidth(); cliprect.top = ypos - TRowHeight(); cliprect.bottom = ypos; clip = cliprect; owner->ConstrainClippingRegion(&clip); locale.DateToString(fDate,string); owner->DrawString(string.String(), BPoint(xpos, ypos - 3)); owner->ConstrainClippingRegion(NULL); xpos += TDateWidth(); owner->SetHighColor(linecolor); //Line Between Date & Type owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos)); owner->StrokeLine(BPoint(0,ypos),BPoint(r.right,ypos)); owner->SetHighColor(0, 0, 0); // Type owner->SetHighColor(0,0,0); owner->DrawString(fType.String(), BPoint(xpos + 5, ypos - 3)); // Line between Type and Payee xpos += TNumWidth(); owner->SetHighColor(linecolor); owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos)); // Calculate the rectangle for the payee, but this field depends on the // width of the view, so we can't yet easily calculate the right coordinate // of the rectangle just yet BRect payee_rect(xpos, ypos, xpos, ypos - TRowHeight()); // Balance xpos = r.right - TAmountWidth(); cliprect.right = r.right; cliprect.left = xpos; clip = cliprect; owner->SetHighColor(0, 0, 0); Fixed balance = fAccount->BalanceAtTransaction(fDate,fPayee.String()); if(balance.AsFixed()<0) owner->SetHighColor(150, 0, 0); locale.CurrencyToString(balance,string); owner->DrawString(string.String(), BPoint(xpos + 5, ypos - 3)); // Line between Balance and Amount owner->SetHighColor(linecolor); owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos)); // Amount xpos -= TAmountWidth(); cliprect.right = cliprect.left; cliprect.left = xpos; clip = cliprect; owner->SetHighColor(0,0,0); fAccount->GetLocale().CurrencyToString(fAmount.AbsoluteValue(),string); owner->ConstrainClippingRegion(&clip); owner->DrawString(string.String(), BPoint(xpos + 5, ypos - 3)); owner->ConstrainClippingRegion(NULL); // Line between Amount and Payee owner->SetHighColor(linecolor); owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos)); // Payee payee_rect.right = xpos; payee_rect.top = ypos - TRowHeight(); payee_rect.bottom = ypos; xpos = payee_rect.left; owner->SetHighColor(0, 0, 0); clip = payee_rect; owner->ConstrainClippingRegion(&clip); owner->DrawString(fPayee.String(), BPoint(xpos + 5, ypos - 3)); owner->ConstrainClippingRegion(NULL); owner->SetHighColor(linecolor); owner->StrokeLine(BPoint(r.left, ypos), BPoint(r.right, ypos)); // Category owner->SetHighColor(0,0,0); ypos += TRowHeight(); xpos = TLeftPadding(); cliprect.left = TLeftPadding(); cliprect.right = r.right / 2; cliprect.top = cliprect.bottom; cliprect.bottom += TRowHeight(); clip = cliprect; owner->ConstrainClippingRegion(&clip); owner->DrawString(fCategory.String(), BPoint(xpos, ypos - 3)); owner->ConstrainClippingRegion(NULL); xpos = r.right / 2; cliprect.left = xpos; cliprect.right = r.right; // Line between Category and Memo owner->SetHighColor(linecolor); owner->StrokeLine(BPoint(xpos, ypos - TRowHeight()), BPoint(xpos, ypos)); // Memo clip = cliprect; owner->ConstrainClippingRegion(&clip); if(fMemo.CountChars()>0) { owner->SetHighColor(0,0,0); owner->DrawString(fMemo.String(), BPoint(xpos + 5, ypos - 3)); } else { owner->SetHighColor(linecolor); owner->DrawString(TRANSLATE("No Memo"), BPoint(xpos + 5, ypos - 3)); } owner->ConstrainClippingRegion(NULL); }
void BudgetWindow::CalcStats(const char *cat, Fixed &high, Fixed &low, Fixed &avg) { if(!cat) return; BString querystring; Fixed cattotal; CppSQLite3Query query; // find the average amount for(int32 j=0; j<gDatabase.CountAccounts(); j++) { Account *acc = gDatabase.AccountAt(j); querystring = "select sum(amount) from account_" ; querystring << acc->GetID() << " where category = '" << EscapeIllegalCharacters(cat) << "';"; query = gDatabase.DBQuery(querystring.String(), "BudgetWindow::CalcStats:get average"); cattotal.AddPremultiplied(query.getInt64Field(0)); query.finalize(); } avg = cattotal; avg /= 12; // find the highest amount cattotal=0; for(int32 j=0; j<gDatabase.CountAccounts(); j++) { Account *acc = gDatabase.AccountAt(j); querystring = "select max(amount) from account_" ; querystring << acc->GetID() << " where category = '" << EscapeIllegalCharacters(cat) << "';"; query = gDatabase.DBQuery(querystring.String(), "BudgetWindow::CalcStats:get highest"); Fixed value; value.SetPremultiplied(query.getInt64Field(0)); cattotal = MAX(cattotal,value); query.finalize(); } high = cattotal; // find the lowest amount cattotal=0; for(int32 j=0; j<gDatabase.CountAccounts(); j++) { Account *acc = gDatabase.AccountAt(j); querystring = "select min(amount) from account_" ; querystring << acc->GetID() << " where category = '" << EscapeIllegalCharacters(cat) << "';"; query = gDatabase.DBQuery(querystring.String(), "BudgetWindow::CalcStats:get highest"); Fixed value; value.SetPremultiplied(query.getInt64Field(0)); cattotal = MIN(cattotal,value); query.finalize(); } low = cattotal; }
void BudgetWindow::GenerateBudget(const bool &zero) { // Generate a budget based on the last year's transactions ReportGrid income(1,0), spending(1,0); gDatabase.DBCommand("delete from budgetlist", "BudgetWindow::GenerateBudget:empty budget"); CppSQLite3Query query; query = gDatabase.DBQuery("select * from categorylist order by name", "BudgetWindow::GenerateBudget:get categories"); if(query.eof()) return; float maxwidth=fCategoryList->StringWidth(TRANSLATE("Category")); while(!query.eof()) { BString catname = DeescapeIllegalCharacters(query.getStringField(0)); if(catname.ICompare(TRANSLATE("Transfer"))==0) { query.nextRow(); continue; } bool isexpense = !query.getIntField(1); if(isexpense) { spending.AddItem(); spending.SetRowTitle(spending.CountItems()-1,catname.String()); } else { income.AddItem(); income.SetRowTitle(income.CountItems()-1,catname.String()); } float tempwidth = fCategoryList->StringWidth(catname.String()); maxwidth = MAX(maxwidth,tempwidth); query.nextRow(); } query.finalize(); // Now that we have the list of categories, query for transactions for each // account from each category BString querystring; Fixed cattotal; for(int32 i=0; i<income.CountItems(); i++) { querystring = ""; cattotal = 0; if(!zero) { for(int32 j=0; j<gDatabase.CountAccounts(); j++) { Account *acc = gDatabase.AccountAt(j); querystring = "select sum(amount) from account_" ; querystring << acc->GetID() << " where category = '" << EscapeIllegalCharacters(income.RowTitle(i)) << "' and date > " << DecrementDateByYear(GetCurrentDate()) << ";"; query = gDatabase.DBQuery(querystring.String(), "BudgetWindow::GenerateBudget:get category"); cattotal.AddPremultiplied(query.getInt64Field(0)); query.finalize(); } cattotal /= 12; cattotal.Round(); } income.SetValue(0,i,cattotal); gDatabase.AddBudgetEntry(BudgetEntry(income.RowTitle(i),cattotal,BUDGET_MONTHLY,false)); } for(int32 i=0; i<spending.CountItems(); i++) { querystring = ""; cattotal = 0; if(!zero) { for(int32 j=0; j<gDatabase.CountAccounts(); j++) { Account *acc = gDatabase.AccountAt(j); querystring = "select sum(amount) from account_" ; querystring << acc->GetID() << " where category = '" << EscapeIllegalCharacters(spending.RowTitle(i)) << "';"; query = gDatabase.DBQuery(querystring.String(), "BudgetWindow::GenerateBudget:get category"); cattotal.AddPremultiplied(query.getInt64Field(0)); query.finalize(); } cattotal /= 12; cattotal.Round(); } spending.SetValue(0,i,cattotal); gDatabase.AddBudgetEntry(BudgetEntry(spending.RowTitle(i),cattotal,BUDGET_MONTHLY,true)); } }
Fixed Fixed::operator-(Fixed const & rhs) { return Fixed(this->toFloat() - rhs.toFloat()); }
void BudgetWindow::MessageReceived(BMessage *msg) { switch(msg->what) { case M_SELECT_CATEGORY: { HandleCategorySelection(); fAmountBox->MakeFocus(true); break; } case M_AMOUNT_CHANGED: { BString str(fAmountBox->Text()); if(str.CountChars()<1) str = "0"; Fixed f; if(gDefaultLocale.StringToCurrency(str.String(),f)!=B_OK) break; f.Round(); gDefaultLocale.CurrencyToString(f,str); str.Truncate(str.FindFirst(gDefaultLocale.CurrencyDecimal())); str.RemoveFirst(gDefaultLocale.CurrencySymbol()); BRow *row = fCategoryList->CurrentSelection(); if(!row) break; row->SetField(new BStringField(str.String()),1); fCategoryList->UpdateRow(row); BudgetEntry entry; gDatabase.GetBudgetEntry( ((BStringField*)row->GetField(0))->String(),entry ); entry.amount = f; if(entry.isexpense) entry.amount.Invert(); gDatabase.AddBudgetEntry(entry); RefreshBudgetGrid(); RefreshBudgetSummary(); fBudgetSummary->SetFocusRow( entry.isexpense ? 1 : 0); fBudgetSummary->SetFocusRow(2); break; } case M_BUDGET_RECALCULATE: { GenerateBudget(false); RefreshBudgetGrid(); RefreshBudgetSummary(); RefreshCategories(); break; } case M_BUDGET_ZERO: { GenerateBudget(true); RefreshBudgetGrid(); RefreshBudgetSummary(); RefreshCategories(); break; } case M_SET_PERIOD_MONTH: { SetPeriod(BUDGET_MONTHLY); break; } case M_SET_PERIOD_WEEK: { SetPeriod(BUDGET_WEEKLY); break; } case M_SET_PERIOD_QUARTER: { SetPeriod(BUDGET_QUARTERLY); break; } case M_SET_PERIOD_YEAR: { SetPeriod(BUDGET_ANNUALLY); break; } case M_NEXT_FIELD: { if(fAmountBox->ChildAt(0)->IsFocus()) fMonthly->MakeFocus(true); break; } case M_PREVIOUS_FIELD: { if(fAmountBox->ChildAt(0)->IsFocus()) fCategoryList->MakeFocus(true); break; } default: BWindow::MessageReceived(msg); } }
Fixed Fixed::operator+(const Fixed& n) const { return (this->toFloat() + n.toFloat()); }
bool Fixed::operator!=(const Fixed& n) const { return (this->toFloat() != n.toFloat()); }
Fixed Fixed::operator/(const Fixed& n) const { if (n.toFloat()) return (this->toFloat() / n.toFloat()); return ((float)0.0); }
inline Fixed Randomize(Fixed range) { return Fixed::from_val(Randomize(range.val())); }
Fixed::Fixed(const Fixed& n) : _fixed(n.getRawBits()), _fractional(8) { std::cout << "Copy constructor called" << std::endl; }