By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,747 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Validating whole numbers stored as float (was "Inheritance Question")

P: n/a
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 "Refactoring -
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***@spammenot.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 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Neils,

"Neils Christoffersen" <ar***@fvb.zvqpb.arg> wrote in message
news:3f**********@newsfeed.slurp.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(float 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

P: n/a
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.zvqpb.arg> wrote in message
news:3f**********@newsfeed.slurp.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 "Refactoring -
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***@spammenot.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 #3

P: n/a
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.zvqpb.arg> wrote in message
news:3f**********@newsfeed.slurp.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 "Refactoring -
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***@spammenot.com> wrote in message
news:Xn*******************************@209.242.8 6.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 discussion thread is closed

Replies have been disabled for this discussion.