Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.clipper.visual-objects > #953 > unrolled thread
| Started by | D.J.W. van Kooten <public@ic2remove.this.com> |
|---|---|
| First post | 2011-04-26 21:33 +0200 |
| Last post | 2011-04-28 22:26 +0000 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.clipper.visual-objects
What's wrong with this cast? D.J.W. van Kooten <public@ic2remove.this.com> - 2011-04-26 21:33 +0200
Re: What's wrong with this cast? "Jamal" <vodotnet_nospam@yahoo.com> - 2011-04-26 16:38 -0400
Re: What's wrong with this cast? D.J.W. van Kooten <public@ic2remove.this.com> - 2011-04-26 22:46 +0200
Re: What's wrong with this cast? "Geoff Schaller" <geoffx@softwarexobjectives.com.au> - 2011-04-26 22:17 +0000
Re: What's wrong with this cast? Robert van der Hulst <E-55525A53-4141-E@heliks.nl> - 2011-04-27 11:42 +0200
Re: What's wrong with this cast? D.J.W. van Kooten <public@ic2remove.this.com> - 2011-04-27 17:41 +0200
Re: What's wrong with this cast? "Geoff Schaller" <geoffx@softwarexobjectives.com.au> - 2011-04-28 22:26 +0000
| From | D.J.W. van Kooten <public@ic2remove.this.com> |
|---|---|
| Date | 2011-04-26 21:33 +0200 |
| Subject | What's wrong with this cast? |
| Message-ID | <s87er6d3aomad9n4sjrh8ucetm5usn9aji@4ax.com> |
In a method where a subtraction is supposed never to yield <0 it did nevertheless as the variable was a dword it gave a run time error. I decided to work with floats inside (realized later that int was better, but that is not the issue now ) and after checking for non zero values, I assigned : nStockDW:=DWORD(_CAST,nStock) where nStock=float nStockDW = dword However: the first value of nStock (1,00) did not yeild dword 1 but a huge number like 43423454554645, and a run time error after use of course. What is wrong with this conversion? Dick
[toc] | [next] | [standalone]
| From | "Jamal" <vodotnet_nospam@yahoo.com> |
|---|---|
| Date | 2011-04-26 16:38 -0400 |
| Message-ID | <ip7ag4$a27$1@dont-email.me> |
| In reply to | #953 |
Try: nStockDW := DWORD(nStock) Jamal "D.J.W. van Kooten" wrote in message news:s87er6d3aomad9n4sjrh8ucetm5usn9aji@4ax.com... In a method where a subtraction is supposed never to yield <0 it did nevertheless as the variable was a dword it gave a run time error. I decided to work with floats inside (realized later that int was better, but that is not the issue now ) and after checking for non zero values, I assigned : nStockDW:=DWORD(_CAST,nStock) where nStock=float nStockDW = dword However: the first value of nStock (1,00) did not yeild dword 1 but a huge number like 43423454554645, and a run time error after use of course. What is wrong with this conversion? Dick
[toc] | [prev] | [next] | [standalone]
| From | D.J.W. van Kooten <public@ic2remove.this.com> |
|---|---|
| Date | 2011-04-26 22:46 +0200 |
| Message-ID | <kober6tt0kkkb1ee0sa6ui93f5d19hme1n@4ax.com> |
| In reply to | #954 |
On Tue, 26 Apr 2011 16:38:25 -0400, "Jamal" <vodotnet_nospam@yahoo.com> wrote: Hello Jamal, >Try: > >nStockDW := DWORD(nStock) Ah! I always thought I needed to add Cast. Indeed this works, thanks. Dick
[toc] | [prev] | [next] | [standalone]
| From | "Geoff Schaller" <geoffx@softwarexobjectives.com.au> |
|---|---|
| Date | 2011-04-26 22:17 +0000 |
| Message-ID | <4db7445e$0$77723$c30e37c6@exi-reader.telstra.net> |
| In reply to | #955 |
The difference is that CAST assumes you know what you're doing and that you know the value is correct for the new data type. DWORD() performs a type conversion and therefore a correction if necessary. To get a huge number, it must have been negative or a fraction. For example 0.999999999 Geoff "D.J.W. van Kooten" <public@ic2remove.this.com> wrote in message news:kober6tt0kkkb1ee0sa6ui93f5d19hme1n@4ax.com: > On Tue, 26 Apr 2011 16:38:25 -0400, "Jamal" > <vodotnet_nospam@yahoo.com> wrote: > > Hello Jamal, > > > >Try: > > > >nStockDW := DWORD(nStock) > > > Ah! I always thought I needed to add Cast. Indeed this works, thanks. > > Dick
[toc] | [prev] | [next] | [standalone]
| From | Robert van der Hulst <E-55525A53-4141-E@heliks.nl> |
|---|---|
| Date | 2011-04-27 11:42 +0200 |
| Message-ID | <19010142267.20110427114237@heliks.nl> |
| In reply to | #953 |
Hi D.J.W., On Tue, 26 Apr 2011, at 21:33:11 [GMT +0200] (which was 21:33 where I live) you wrote about: 'What's wrong with this cast?' > In a method where a subtraction is supposed never to yield <0 it did > nevertheless as the variable was a dword it gave a run time error. > I decided to work with floats inside (realized later that int was > better, but that is not the issue now ) and after checking for non > zero values, I assigned : > nStockDW:=DWORD(_CAST,nStock) > where nStock=float > nStockDW = dword > However: the first value of nStock (1,00) did not yeild dword 1 but a > huge number like 43423454554645, and a run time error after use of > course. > What is wrong with this conversion? if nStock is a float, then you are casting the address of the float to a DWORD. Floats are a reference type in VO. You can easily see that by doing the cast twice and running CollectForced() in between: nStockDW:=DWORD(_CAST,nStock) CollectForced() nStockDW:=DWORD(_CAST,nStock) You will see that the second cast returns a different value. Like Jamal said: nStockDW := DWORD(nStock) will do the trick. However when nStock is negative then this could throw a runtime error (depending on the overflow compiler setting) -- Robert van der Hulst AKA Mr. Data www.heliks.nl
[toc] | [prev] | [next] | [standalone]
| From | D.J.W. van Kooten <public@ic2remove.this.com> |
|---|---|
| Date | 2011-04-27 17:41 +0200 |
| Message-ID | <c0egr6db2rfmogg30b095d9rl0eh8qf3kg@4ax.com> |
| In reply to | #963 |
On Wed, 27 Apr 2011 11:42:37 +0200, Robert van der Hulst <E-55525A53-4141-E@heliks.nl> wrote: Hello Robert, >if nStock is a float, then you are casting the address of the float to a DWORD. >Floats are a reference type in VO. You can easily see that by doing the cast >twice and running CollectForced() in between: > >nStockDW:=DWORD(_CAST,nStock) >CollectForced() >nStockDW:=DWORD(_CAST,nStock) > >You will see that the second cast returns a different value. Thanks for the explanation. It is no longer your responsibility, but do you agree this information should have been in the help, subject: "Type Casting and Conversion Operators" It says: Their general format is: <idType>(_CAST, <Value>) where <idType> is one of the data types listed above. DWORD and FLOAT are in that list without mentioning that it doesn't work. Even better would have been to add DWORD(_CAST,nFloat) under the option to create random numbers. Random is not working either and this cast would do a great job I now understand <g>. >However when nStock is negative then this could throw a runtime error (depending >on the overflow compiler setting) That's why I did this in the first place. Any negative float is set to 0 if it's <0 before the dword conversion. Dick
[toc] | [prev] | [next] | [standalone]
| From | "Geoff Schaller" <geoffx@softwarexobjectives.com.au> |
|---|---|
| Date | 2011-04-28 22:26 +0000 |
| Message-ID | <4db9e990$0$77723$c30e37c6@exi-reader.telstra.net> |
| In reply to | #966 |
<rofl> Dick, the problem is that dword(_cast, fFloat) DOES work, provided you understand the context. It doesn't convert the contents of the float, just its pointer, and that could be quite valid. So the help instructions are correct when read in the context they are written. Over the years (and at conferences) there have been numerous warnings about casting vs conversion functions so I am a little surprised you got caught - you were there. Robert covered this very point in his VO Voodoo sessions. Unfortunately you can't make help instructions idiot proof because the world can always make a better class of idiot <g>. Geoff "D.J.W. van Kooten" <public@ic2remove.this.com> wrote in message news:c0egr6db2rfmogg30b095d9rl0eh8qf3kg@4ax.com: > On Wed, 27 Apr 2011 11:42:37 +0200, Robert van der Hulst > <E-55525A53-4141-E@heliks.nl> wrote: > > Hello Robert, > > > >if nStock is a float, then you are casting the address of the float to a DWORD. > >Floats are a reference type in VO. You can easily see that by doing the cast > >twice and running CollectForced() in between: > > > >nStockDW:=DWORD(_CAST,nStock) > >CollectForced() > >nStockDW:=DWORD(_CAST,nStock) > > > >You will see that the second cast returns a different value. > > > Thanks for the explanation. It is no longer your responsibility, but > do you agree this information should have been in the help, subject: > > "Type Casting and Conversion Operators" > > It says: > > Their general format is: > <idType>(_CAST, <Value>) > where <idType> is one of the data types listed above. > > DWORD and FLOAT are in that list without mentioning that it doesn't > work. > > Even better would have been to add DWORD(_CAST,nFloat) under the > option to create random numbers. Random is not working either and this > cast would do a great job I now understand <g>. > > > >However when nStock is negative then this could throw a runtime error (depending > >on the overflow compiler setting) > > > That's why I did this in the first place. Any negative float is set to > 0 if it's <0 before the dword conversion. > > Dick
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.clipper.visual-objects
csiph-web