Discussion:
Mod operator in Delphi 2007
(too old to reply)
Stark
2010-03-26 14:52:35 UTC
Permalink
I am trying to use the operator "MOD" in Delphi 2007 and I get an error
message which I don't understand. Anyone seeing waht's wrong ?

var
res, resf: double;
...........................
resf:= (QrySchedule.FieldByName('ALARMSTART').AsDateTime - aDate);
res := (resf mod 7); "operator not applicable to this operand type"

PS: If I filter the CodegerHelpFile for Delphi WIn32, the strange thing is
that "mod" is not there..
Marco van de Voort
2010-03-26 15:16:04 UTC
Permalink
Post by Stark
I am trying to use the operator "MOD" in Delphi 2007 and I get an error
message which I don't understand. Anyone seeing waht's wrong ?
var
res, resf: double;
...........................
resf:= (QrySchedule.FieldByName('ALARMSTART').AsDateTime - aDate);
res := (resf mod 7); "operator not applicable to this operand type"
mod is an integer operator, while you use it if it were a floating point
operator.
Post by Stark
PS: If I filter the CodegerHelpFile for Delphi WIn32, the strange thing is
that "mod" is not there..
It is not a function but an operator. Make sure you look in the right
section. (it is there in both my d2006 as 2009)
Jamie
2010-03-26 22:12:46 UTC
Permalink
Post by Stark
I am trying to use the operator "MOD" in Delphi 2007 and I get an error
message which I don't understand. Anyone seeing waht's wrong ?
var
res, resf: double;
...........................
resf:= (QrySchedule.FieldByName('ALARMSTART').AsDateTime - aDate);
res := (resf mod 7); "operator not applicable to this operand type"
PS: If I filter the CodegerHelpFile for Delphi WIn32, the strange thing
is that "mod" is not there..
Doubles/Floats are a different storage type..

You must convert over to a integer type to perform that.

res := Round(resf) MOD 7;


The "ROUND" function generates a integer style that can work with the
"MOD" operator.. RES being a float style, will get reassigned back to
a float as the results..

The compiler will load a Float type from binary types and other
floats of course how ever, going the other way around, you need to use
functions to do the conversion from a FLoat to a binary type.

MOD works with binary types the last time I knew.. I suppose one could
write a function to make it perform with floats directly..

You also have the "Trunc" function that will generate an integer
value but it'll always round down..
Stark
2010-03-26 21:45:39 UTC
Permalink
Yes, finally the expression works. I searched the Help file for a procedure
to convert float to integer that I knew it existed, but could not find it. I
don't know why, but the CodeGear Help just doesn't show neither 'round' nor
'trunc', and 'Mod' is only shown if the filterr for Delphi Win32 is not set.
Ther must be something wrong or what ?

I had to go back to Delphi6 help, whic I still have on this machine, to find
them.
Post by Jamie
Post by Stark
I am trying to use the operator "MOD" in Delphi 2007 and I get an error
message which I don't understand. Anyone seeing waht's wrong ?
var
res, resf: double;
...........................
resf:= (QrySchedule.FieldByName('ALARMSTART').AsDateTime - aDate);
res := (resf mod 7); "operator not applicable to this operand type"
PS: If I filter the CodegerHelpFile for Delphi WIn32, the strange thing
is that "mod" is not there..
Doubles/Floats are a different storage type..
You must convert over to a integer type to perform that.
res := Round(resf) MOD 7;
The "ROUND" function generates a integer style that can work with the
"MOD" operator.. RES being a float style, will get reassigned back to
a float as the results..
The compiler will load a Float type from binary types and other floats
of course how ever, going the other way around, you need to use
functions to do the conversion from a FLoat to a binary type.
MOD works with binary types the last time I knew.. I suppose one could
write a function to make it perform with floats directly..
You also have the "Trunc" function that will generate an integer value
but it'll always round down..
Rudy Velthuis
2010-03-26 23:39:41 UTC
Permalink
Post by Stark
Yes, finally the expression works. I searched the Help file for a
procedure to convert float to integer that I knew it existed, but
could not find it. I don't know why, but the CodeGear Help just
doesn't show neither 'round' nor 'trunc', and 'Mod' is only shown if
the filterr for Delphi Win32 is not set.
This content can also be found in the help:

http://docwiki.embarcadero.com/RADStudio/en/Expressions_%28Delphi%29#Arithmetic_Operators

Same for Round and Trunc:

http://docwiki.embarcadero.com/VCL/en/System.Round
http://docwiki.embarcadero.com/VCL/en/System.Trunc
--
Rudy Velthuis http://rvelthuis.de

"Do not mind anything that anyone tells you about anyone else.
Judge everyone and everything for yourself."
-- Henry James
a***@aol.com
2010-03-28 07:35:15 UTC
Permalink
Post by Stark
Yes, finally the expression works. I searched the Help file for a procedure
to convert float to integer that I knew it existed, but could not find it. I
don't know why, but the CodeGear Help just doesn't show neither 'round' nor
'trunc', and 'Mod' is only shown if the filterr for Delphi Win32 is not set.
Ther must be something wrong or what ?
I think one needs to be familiar with all the type-modifying & value-
modifying operators & arithmetic functions -

Mod, Div, Trunc, Int, Frac, Round, Abs, etc

But few programs include Bankers (or Gaussian) Rounding - round to
nearest even rounding level, not even Excel.

Alan Lloyd
Maarten Wiltink
2010-03-27 09:50:20 UTC
Permalink
"Jamie" <***@charter.net> wrote in message news:ir9rn.2903$***@newsfe10.iad...
[...]
Post by Jamie
The compiler will load a Float type from binary types and other
floats of course how ever, going the other way around, you need to use
functions to do the conversion from a FLoat to a binary type.
They're all binary types. I take it you mean 'ordinal', 'integer',
or 'fixed point'.


[...]
Post by Jamie
You also have the "Trunc" function that will generate an integer
value but it'll always round down..
Which is why it's named after truncation. There's Int, Round, and Trunc,
and they simply do what they were meant and documented to do. There's
nothing 'but' about it; if any one function doesn't do what you want,
use (or build) the one that does. It's not rocket science.

Groetjes,
Maarten Wiltink
Jamie
2010-03-27 15:20:59 UTC
Permalink
Post by Maarten Wiltink
[...]
Post by Jamie
The compiler will load a Float type from binary types and other
floats of course how ever, going the other way around, you need to use
functions to do the conversion from a FLoat to a binary type.
They're all binary types. I take it you mean 'ordinal', 'integer',
or 'fixed point'.
[...]
Post by Jamie
You also have the "Trunc" function that will generate an integer
value but it'll always round down..
Which is why it's named after truncation. There's Int, Round, and Trunc,
and they simply do what they were meant and documented to do. There's
nothing 'but' about it; if any one function doesn't do what you want,
use (or build) the one that does. It's not rocket science.
Groetjes,
Maarten Wiltink
With our present day level of students being pushed out the
door just to collect tuition, non rocket science is becoming a challenge.

And yes, when I stated binary, it was in the context of non fractional
native data types. I think most with half a brain can figure that out..
If not, maybe they're in the wrong business/hobby or should be
asking/seeking very basic help on the subject.

I'm a firm believer of elementary education on subjects to avoid the
aggravation that can result from skipping over those basic needs..

I started with digital electronics, punch cards, hex keypads inputs etc..
Not saying one has to go that low to learn, because you can learn
those things with educational software tools now days. But on should
understand those level's before undergoing a serious level of coding.

It is my opinion, if that was the case for most young pro claimed
programmers out there, things like .NET would have never survived! Which
is another sore subject for me. (Bloat) and more (Bloat), wasted use of
CPU time ect...






Jamie.
Maarten Wiltink
2010-03-27 14:51:04 UTC
Permalink
"Jamie" <***@charter.net> wrote in message news:cvorn.13828$***@newsfe15.iad...
[...]
Post by Jamie
I'm a firm believer of elementary education on subjects to avoid the
aggravation that can result from skipping over those basic needs..
I started with digital electronics, punch cards, hex keypads inputs etc..
Not saying one has to go that low to learn, because you can learn
those things with educational software tools now days. But on should
understand those level's before undergoing a serious level of coding.
It is my opinion, if that was the case for most young pro claimed
programmers out there, things like .NET would have never survived!
Which is another sore subject for me. (Bloat) and more (Bloat), wasted
use of CPU time ect...
I learned to program computers before I ever learned how they're built.
But in university they told us _how_ they worked on the inside, too,
and it made me a better programmer.

There is so much CPU to waste now that I don't mind seeing it being
blown on frameworks and class libraries, as long as they have decent
help files. Rather that than OS shells that look like video games.

If every programmer were a formally trained, competent programmer, I
believe there would still be a .Net. It might even be more widely used.
Everyone using it might actually be qualified to fix bugs in it.

Christ, I'm getting old. And cranky.

Groetjes,
Maarten Wiltink
Hans-Peter Diettrich
2010-03-27 20:57:03 UTC
Permalink
Post by Jamie
And yes, when I stated binary, it was in the context of non fractional
native data types.
That's "integral" numbers.

DoDi
Jamie
2010-03-27 23:58:49 UTC
Permalink
Post by Hans-Peter Diettrich
Post by Jamie
And yes, when I stated binary, it was in the context of non fractional
native data types.
That's "integral" numbers.
DoDi
yeah? what about BYTE, WORD, DWORD, QWORD and so on.

Integers are higher level representation of those. signed
or unsigned. still the same.
and let us net forget pointers.

The only difference here is occupied memory and how you want
to perceive them at a higher level of usage.
Rudy Velthuis
2010-03-28 07:24:10 UTC
Permalink
Post by Jamie
Post by Hans-Peter Diettrich
Post by Jamie
And yes, when I stated binary, it was in the context of non
fractional native data types.
That's "integral" numbers.
DoDi
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
--
Rudy Velthuis http://rvelthuis.de

"Time is the best teacher; Unfortunately it kills all its
students!" -- bumper sticker
Rudy Velthuis
2010-03-28 07:29:51 UTC
Permalink
Post by Rudy Velthuis
Post by Jamie
Post by Hans-Peter Diettrich
Post by Jamie
And yes, when I stated binary, it was in the context of non
fractional native data types.
That's "integral" numbers.
DoDi
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
FWIW, in Delphi, operators like div, mod and the bitwise operators only
work on integral types. They also work on Bytes, Words, etc. because in
Delphi, these are integral types.
--
Rudy Velthuis http://rvelthuis.de

"Nothing overshadows truth so much as authority."
-- Leon Battista Alberti
Marco van de Voort
2010-03-28 14:35:13 UTC
Permalink
Post by Rudy Velthuis
Post by Jamie
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
Hmm, I'd say:

since integer and pointer are non compatible, you can't store a pointer in
an integer. You might reuse the memory used to store the integer for other
purposes though, if large enough.
Jamie
2010-03-28 17:18:28 UTC
Permalink
Post by Marco van de Voort
Post by Rudy Velthuis
Post by Jamie
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
since integer and pointer are non compatible, you can't store a pointer in
an integer. You might reuse the memory used to store the integer for other
purposes though, if large enough.
What?>
Rudy Velthuis
2010-03-28 18:56:05 UTC
Permalink
Post by Marco van de Voort
Post by Rudy Velthuis
Post by Jamie
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
since integer and pointer are non compatible, you can't store a
pointer in an integer.
But you can. It just depends on the size of the pointer and the integer
which integral type you need.
--
Rudy Velthuis http://rvelthuis.de

"You can only find truth with logic if you have already found
truth without it." -- Gilbert Keith Chesterton (1874-1936)
Marco van de Voort
2010-03-29 04:41:01 UTC
Permalink
Post by Rudy Velthuis
Post by Marco van de Voort
that.
since integer and pointer are non compatible, you can't store a
pointer in an integer.
But you can. It just depends on the size of the pointer and the integer
which integral type you need.
How? I only know solutions using casting, which is overriding the
typesystem, and reusing the memory more or less.
Rudy Velthuis
2010-03-29 10:37:33 UTC
Permalink
This post might be inappropriate. Click to display it.
BRoberts
2010-03-30 16:29:46 UTC
Permalink
Post by Rudy Velthuis
Post by Marco van de Voort
Post by Rudy Velthuis
Post by Jamie
yeah? what about BYTE, WORD, DWORD, QWORD and so on.
They are all (unsigned) integers. That they can be used to hold
pointers or other data types like sets or enums, too, doesn't change
that.
since integer and pointer are non compatible, you can't store a
pointer in an integer.
But you can. It just depends on the size of the pointer and the integer
which integral type you need.
No you can not. Given integer variable i

i := @i;

is not a legal statement in Delphi. You can, however, coerce the data or
looked at another way temporarily change the data type of the storage. The
distinction is important. In fact it is one of the fundamental guiding
principals of Pascal and thus Delphi.
Rudy Velthuis
2010-03-31 00:03:49 UTC
Permalink
Post by BRoberts
Post by Rudy Velthuis
Post by Marco van de Voort
since integer and pointer are non compatible, you can't store a
pointer in an integer.
But you can. It just depends on the size of the pointer and the
integer which integral type you need.
No you can not.
Of course I can:

MyLongint := Longint(YourPointer);

I'm not saying that this is maintainable or pretty code, but of course
you CAN STORE a pointer in an integral type, if both have matching
sizes (note that I am not talking about ASSIGNMENT, which involves
assignment-compatible types and may even require a conversion). Memory
is memory, and 4 bytes are 4 bytes (assuming Win32). How the contents
of these bytes are used is merely a matter of interpretation.

As I said before, the types WORD, DWORD, QWORD etc. merely denote 2
byte, 4 byte, 8 byte etc. memory locations. In Delphi, we treat them as
integral types, but in reality, they stand for raw memory, not just for
numbers.
--
Rudy Velthuis http://rvelthuis.de

"Computers are useless; they can only give you answers."
-- Pablo Picasso
Rudy Velthuis
2010-03-31 00:05:15 UTC
Permalink
The distinction is important. In fact it is one of the
fundamental guiding principals of Pascal and thus Delphi.
I'm new here, I didn't know this, sorry.
--
Rudy Velthuis http://rvelthuis.de

Wood's Axiom: As soon as a still-to-be-finished computer task
becomes a life-or-death situation, the power fails.
a***@aol.com
2010-03-28 07:05:52 UTC
Permalink
On 27 Mar, 16:20, Jamie
<***@charter.net> wrote:
<snip>
� �I'm a firm believer of elementary education on subjects to avoid the
aggravation that can result from skipping over those basic needs..
<snip>

Careful, you're getting near a hobby herse of mine. Tuition often
teaches by describing the effects and not the basic concepts. One is
left to infer the concepts from the effects. This error is manifest in
most computer technical books and leaves one hopelessly lost if the
effects don't always work as they should.

On 27 Mar, 15:51, "Maarten Wiltink" <***@kittensandcats.net>
wrote:
<snip>
Christ, I'm getting old. And cranky.
I don't know about the cranky bit, but I doubt if you're as old as I.
I've even coded in Forth, and I'll be 80 this year.

Alan Lloyd
Rudy Velthuis
2010-03-28 07:20:39 UTC
Permalink
Post by a***@aol.com
I don't know about the cranky bit, but I doubt if you're as old as I.
I've even coded in Forth, and I'll be 80 this year.
80? Now that is indeed a respectable age.

FWIW, I'm only 49, but I've also coded in Forth. <g>
--
Rudy Velthuis http://rvelthuis.de

"More computing sins are committed in the name of efficiency
(without necessarily achieving it) than for any other single
reason - including blind stupidity." -- W.A. Wulf
Maarten Wiltink
2010-03-28 12:52:38 UTC
Permalink
Post by a***@aol.com
Post by Maarten Wiltink
Christ, I'm getting old. And cranky.
I don't know about the cranky bit, but I doubt if you're as old as I.
I've even coded in Forth, and I'll be 80 this year.
I stand in awe.

Not of the Forth bit. I could do Forth if I wanted to. I may yet.

I'll be forty this year. I now seriously wonder what I'll be programming
when I'm your age, and what I'll be telling the tadpoles then.

Groetjes,
Maarten Wiltink
Paul E. Schoen
2010-03-28 15:15:51 UTC
Permalink
Post by Maarten Wiltink
Post by a***@aol.com
Post by Maarten Wiltink
Christ, I'm getting old. And cranky.
I don't know about the cranky bit, but I doubt if you're as old as I.
I've even coded in Forth, and I'll be 80 this year.
I stand in awe.
Not of the Forth bit. I could do Forth if I wanted to. I may yet.
I'll be forty this year. I now seriously wonder what I'll be programming
when I'm your age, and what I'll be telling the tadpoles then.
FLOOR( My.age) = ( CEIL(Alan.Age) + CEIL(Maarten.Age) ) DIV 2

I learned computers starting in 1966 and it included the nuts and bolts of
accumulator and memory design. We had some exercises in IBM 7094 assembly
and FORTRAN, and we also learned AFBIC, which was All Fortran BASIC
Interpretive Compiler. Code was entered on punch cards and submitted in
batches, and we got printed results on wide folded paper. Each of us got
something like 2 minutes of CPU time, which was generous considering it was
probably at least $20/minute in 1966 dollars. Most projects used only about
5-10 seconds.

Paul
Loading...