473,666 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating whole numbers stored as float (was "Inheritanc e Question")

Hey all,

I wrote on Friday asking for some help moving a common subclass field up
to the base class (original post and followup included below). This
entails storing whole numbers inside float data types. What I'm stuck on
is how to make sure that the value coming in is indeed a whole number,
without converting it to a string and doing manipulation that way. Is
there a mathematical way to do this?

Thanks again for your help.

--
Neils Christoffersen
ar***@fvb.zvqpb .arg (ROT13 to decode)
Original thread:

Neils,
How can I accomplish this?
You can use the Pull Up Field refactoring to move the common field from each
child class into the parent class.

http://www.refactoring.com/catalog/pullUpField.html

However before you do that, or while you do that you will need to ensure
that the field is the same type or compatible type (same base type) in each
class. Seeing as an int will fit in a float, I would make the field float
when I pulled it up.

I would consider having a virtual property that the two classes where the
value is an int can override the property to ensure the value stored is
stored as a whole number. This property would be of type float, like the
field. (this would be based on actual business rules).

Closely related to the Pull Up Field refactoring is the Pull Up Method
refactoring:

http://www.refactoring.com/catalog/pullUpMethod.html

To effectively use Refactoring, Martin Fowler's book "Refactorin g -
Improving the Design of Existing Code" from Addison Wesley is a must, as he
goes into details on the how & why of each refactoring.

http://www.refactoring.com

Hope this helps
Jay

"Neils Christoffersen" <ne***@spammeno t.com> wrote in message
news:Xn******** *************** ********@209.24 2.86.10...
Hey Group,

I have an abstract base class and four child classes that inherit from it. All child classes have a data member called _changeAmount. In two class
this member is an int and in the other two it is a float.

I'd like to somehow move _changeAmount to the base class, because all the child operations that use it are the same. I know this would be simple to do if each child used the same data type, but I'm not sure how to go about it seeing that they are not the same.

How can I accomplish this?

Thanks in advance,
Neils Christoffersen


Nov 15 '05 #1
3 4952
Hi Neils,

"Neils Christoffersen" <ar***@fvb.zvqp b.arg> wrote in message
news:3f******** **@newsfeed.slu rp.net...
Hey all,

I wrote on Friday asking for some help moving a common subclass field up
to the base class (original post and followup included below). This
entails storing whole numbers inside float data types. What I'm stuck on
is how to make sure that the value coming in is indeed a whole number,
without converting it to a string and doing manipulation that way. Is
there a mathematical way to do this?


Perhaps something like this:

public bool IsWholeNumber(f loat value)
{
return ((value % 1) == 0);
}

You'll probably also want to make sure the value is between
Int32.MaxValue and Int32.MinValue.

Regards,
Dan
Nov 15 '05 #2
Neils,
In addition to the % (remainder) operator, you might be able to use some of
the functions in System.Math (such as Math.Floor and Math.Ceiling) as I
would expect if the floor of the number matches the ceiling of a number then
the number must be a whole number. (within a small margin of error as
floating point numbers are inexact).

Also as Daniel stated, checking Int32.MinValue & Int32.MaxValue after the
above check is a good idea.

Hope this helps
Jay

"Neils Christoffersen" <ar***@fvb.zvqp b.arg> wrote in message
news:3f******** **@newsfeed.slu rp.net...
Hey all,

I wrote on Friday asking for some help moving a common subclass field up
to the base class (original post and followup included below). This
entails storing whole numbers inside float data types. What I'm stuck on
is how to make sure that the value coming in is indeed a whole number,
without converting it to a string and doing manipulation that way. Is
there a mathematical way to do this?

Thanks again for your help.

--
Neils Christoffersen
ar***@fvb.zvqpb .arg (ROT13 to decode)
Original thread:

Neils,
>> How can I accomplish this?
You can use the Pull Up Field refactoring to move the common field from each child class into the parent class.

http://www.refactoring.com/catalog/pullUpField.html

However before you do that, or while you do that you will need to ensure
that the field is the same type or compatible type (same base type) in each class. Seeing as an int will fit in a float, I would make the field float
when I pulled it up.

I would consider having a virtual property that the two classes where the
value is an int can override the property to ensure the value stored is
stored as a whole number. This property would be of type float, like the
field. (this would be based on actual business rules).

Closely related to the Pull Up Field refactoring is the Pull Up Method
refactoring:

http://www.refactoring.com/catalog/pullUpMethod.html

To effectively use Refactoring, Martin Fowler's book "Refactorin g -
Improving the Design of Existing Code" from Addison Wesley is a must, as he goes into details on the how & why of each refactoring.

http://www.refactoring.com

Hope this helps
Jay

"Neils Christoffersen" <ne***@spammeno t.com> wrote in message
news:Xn******** *************** ********@209.24 2.86.10...
>> Hey Group,
>>
>> I have an abstract base class and four child classes that inherit from it. >> All child classes have a data member called _changeAmount. In two class >> this member is an int and in the other two it is a float.
>>
>> I'd like to somehow move _changeAmount to the base class, because all the >> child operations that use it are the same. I know this would be simple to >> do if each child used the same data type, but I'm not sure how to go about >> it seeing that they are not the same.
>>
>> How can I accomplish this?
>>
>> Thanks in advance,
>> Neils Christoffersen

Nov 15 '05 #3
My thanks to Daniel and Jay. Your help is greatly appreciated.

--
Neils Christoffersen
ar***@fvb.zvqpb .arg (ROT13 to decode)

Jay B. Harlow [MVP - Outlook] wrote:
Neils,
In addition to the % (remainder) operator, you might be able to use some of
the functions in System.Math (such as Math.Floor and Math.Ceiling) as I
would expect if the floor of the number matches the ceiling of a number then
the number must be a whole number. (within a small margin of error as
floating point numbers are inexact).

Also as Daniel stated, checking Int32.MinValue & Int32.MaxValue after the
above check is a good idea.

Hope this helps
Jay

"Neils Christoffersen" <ar***@fvb.zvqp b.arg> wrote in message
news:3f******** **@newsfeed.slu rp.net...
Hey all,

I wrote on Friday asking for some help moving a common subclass field up
to the base class (original post and followup included below). This
entails storing whole numbers inside float data types. What I'm stuck on
is how to make sure that the value coming in is indeed a whole number,
without converting it to a string and doing manipulation that way. Is
there a mathematical way to do this?

Thanks again for your help.

--
Neils Christoffersen
ar***@fvb.zvq pb.arg (ROT13 to decode)
Original thread:

Neils,
>> How can I accomplish this?


You can use the Pull Up Field refactoring to move the common field from


each
child class into the parent class.

http://www.refactoring.com/catalog/pullUpField.html

However before you do that, or while you do that you will need to ensure
that the field is the same type or compatible type (same base type) in


each
class. Seeing as an int will fit in a float, I would make the field float
when I pulled it up.

I would consider having a virtual property that the two classes where the
value is an int can override the property to ensure the value stored is
stored as a whole number. This property would be of type float, like the
field. (this would be based on actual business rules).

Closely related to the Pull Up Field refactoring is the Pull Up Method
refactoring :

http://www.refactoring.com/catalog/pullUpMethod.html

To effectively use Refactoring, Martin Fowler's book "Refactorin g -
Improving the Design of Existing Code" from Addison Wesley is a must, as


he
goes into details on the how & why of each refactoring.

http://www.refactoring.com

Hope this helps
Jay

"Neils Christoffersen" <ne***@spammeno t.com> wrote in message
news:Xn****** *************** **********@209. 242.86.10...
>> Hey Group,
>>
>> I have an abstract base class and four child classes that inherit

from it.
>> All child classes have a data member called _changeAmount. In two
class
>> this member is an int and in the other two it is a float.
>>
>> I'd like to somehow move _changeAmount to the base class, because

all the
>> child operations that use it are the same. I know this would be

simple to
>> do if each child used the same data type, but I'm not sure how to go

about
>> it seeing that they are not the same.
>>
>> How can I accomplish this?
>>
>> Thanks in advance,
>> Neils Christoffersen


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
3173
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
1
1617
by: AmiciDan | last post by:
When I attempt to create a new project in Visual Studio .NET 2003, I do NOT have the option to create the following: Microsoft Office 2003 Projects What do I need to install or do to get this option as a project type? I have a Universal MSDN subscription and am using Visual Studio .NET 2003 Enterprise Edition. I attempted to re-run the setup, but I did not see "Microsoft Office 2003 Projects" as an option.
6
1608
by: Adam Smith | last post by:
I have posted this and similar questions repeatedly and can't even raise a single response. I am being led to believe that this then 'Must be a stupid question' although people say that there is no stupid question. Is that another for political correctness I am attempting an install of 7.4.3 on FreeBSD O/S 4.9, apparently remnants of 7.3.x are scattered around on the disk from (a) previous ports installation, causing mutex_lock/unlock,...
1
4932
by: Anurag | last post by:
Hi, I have 2 related questions. DB2 UDB ESE v8.x (8.1 till 8.2 FP2 - all fixpaks included) on AIX 5.4.x _____________________________________________________________________________ (QUESTION 1) Output of "db2ilist" does not list all the instances on my AIX box. DB2 functions normally - I do not get any unexpected errors. Any ideas what configuration has been missed out?...
4
1764
by: J.M. | last post by:
I have a question concerning inheritance: can an abstract (parent) class have an abstract object? I would like to make a concrete child inherit from this class by inheriting from this object. Let me try to make this clear by example: The following should be "abstract". "class motor" is abstract because it has a virtual member "virtual int calculate_horsepower() = 0" "class car" should have "motor M;"
42
3429
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
3
18581
by: darkenergy | last post by:
Hi all, I want to do a similar thing as class inheritance in C++, but with structures in C. I have a structure A, and I want to define a second structure B (in different .h/.c files), which has the same variables as A plus some additional ones. I want to use the existing functions that manipulate and create A also for B and, using some new functions, add the bits about the additional variables. If possible, it will not be necessary to...
5
2176
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you decide if a number is a prime or not import math num = int(raw_input("Please enter a number: ")) #num = int(math.sqrt(num))
1
1753
by: Pallav singh | last post by:
Hi i find Following statement while reading Design pattern in C++ ( author - Erich Gamma) "Inheritance break Encapsulation " because inheritance exposes to subclass the detail of its parent's Implementation Is it correct ??? as we only expose interface ( function prototype) to derived class But not how it being implemented
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.