Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Real.cc

Go to the documentation of this file.
00001 /*
00002  * Real.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Native double wrapper.
00006  *
00007  * Copyright (c) 2004 Martin Dvorak, Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stlib/Real.h>
00025 
00026 #include <stlib/Integer.h>
00027 #include <stlib/String.h>
00028 #include <stlib/Stream.h>
00029 
00030 using namespace Core;
00031 
00032 Real::Real(double v)
00033 {
00034     _value = v;
00035 }
00036 
00037 /* Class-accessing protocol */
00038 String *Real::className(void) const
00039 {
00040     return new String("Real");
00041 }
00042 
00043 /* Instance creation protocol */
00044 Real *Real::value(double val)
00045 {
00046     return new Real(val);
00047 }
00048 
00049 /* Converting protocol */
00050 double Real::asDouble(void) const
00051 {
00052     return _value;
00053 }
00054 
00055 long long Real::asLongLong(void) const
00056 {
00057     return (long long) _value;
00058 }
00059 
00060 Integer *Real::rounded(void) const
00061 {
00062     return Integer::value((long long) (_value + 0.5));
00063 }
00064 
00065 Integer *Real::truncated(void) const
00066 {
00067     return Integer::value(this->asLongLong());
00068 }
00069 
00070 /* Comparing protocol */
00071 long Real::hash(void) const
00072 {
00073     return (long) _value;
00074 }
00075 
00076 bool Real::isEqual(const Object *object) const
00077 {
00078     if (!object->isNumber()) return false;
00079 
00080     return ((Number *) object)->isEqualToDouble(_value);
00081 }
00082 
00083 /* Double dispatching protocol */
00084 bool Real::isEqualToLongLong(long long arg) const
00085 {
00086     return _value == arg;
00087 }
00088 
00089 bool Real::isEqualToDouble(double arg) const
00090 {
00091     return _value == arg;
00092 }
00093 
00094 /* Evaluating protocol */
00095 Number *Real::negate(void)
00096 {
00097     if (isZero()) return this;
00098     return Real::value(-_value);
00099 }
00100 
00101 /* Printing protocol */
00102 void Real::printOn(Stream *stream) const
00103 {
00104     char number[30];
00105     sprintf(number, "%f", _value);
00106     stream->nextPutAll(number);
00107 }
00108 
00109 /* Testing protocol */
00110 bool Real::isReal(void) const
00111 {
00112     return true;
00113 }
00114 
00115 bool Real::isPositive(void) const
00116 {
00117     return _value >= 0;
00118 }
00119 
00120 bool Real::isZero(void) const
00121 {
00122     return _value == 0;
00123 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2