DataSection and Reading Data items issue

Just starting out? Need help? Post your questions and find answers here.
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

DataSection and Reading Data items issue

Post by simberr »

I am building more knowledge on PB and am having a lot of fun with it.

I have an issue with using the DataSection. I have this datasection:

Code: Select all

  DataSection
    NumberLives:
    Data.i 100000,99362,99317,99289,99266,99249,99234,99219,99206,99194,99184,99174,99164,99151,99131,99100,99059,99006,98941,98863,98771
    Data.i 98663,98542,98410,98272,98131,97989,97844,97698,97547,97393,97235,97072,96906,96736,96562,96383,96198,96006,95809,95603,95389
    Data.i 95164,94925,94671,94397,94102,93784,93436,93054,92632,92168,91659,91103,90501,89851,89150,88396,87588,86724,85802,84819,83772
    Data.i 82663,81498,80277,78995,77644,76216,74704,73100,71393,69574,67640,65592,63426,61130,58693,56117,53406,50564,47585,44475,41251
    Data.i 37939,34566,31158,27748,24374,21079,17915,14934,12186,9714,7549,5706,4193,2996,2083,1410,932,600,376,228
    Data.i 124,76,42,22,11,5,2,1,0,0
  EndDataSection
  DataSection
    AgeTo:
    Data.d 76.15,75.63,74.67,73.69,72.71,71.72,70.73,69.74,68.75,67.76,66.76,65.77,64.78,63.79,62.80,61.82,60.84,59.88,58.91,57.96
    Data.d 57.01,56.08,55.14,54.22,53.29,52.37,51.44,50.52,49.59,48.67,47.75,46.82,45.90,44.98,44.06,43.14,42.22,41.30,40.38,39.46
    Data.d 38.54,37.63,36.72,35.81,34.90,34.00,33.11,32.22,31.34,30.46,29.60,28.75,27.90,27.07,26.25,25.43,24.63,23.83,23.05,22.27
    Data.d 21.51,20.75,20.00,19.27,18.53,17.81,17.09,16.38,15.68,14.98,14.30,13.63,12.97,12.33,11.70,11.08,10.48,9.89,9.33,8.77
    Data.d 8.24,7.72,7.23,6.75,6.30,5.87,5.45,5.06,4.69,4.35,4.03,3.73,3.46,3.21,2.99,2.80,2.63,2.48,2.34,2.22
    Data.d 2.11,2.00,1.89,1.79,1.69,1.59,1.50,1.41,1.33,1.25,1.17,1.10,1.03,0.96,0.89,0.83,0.77,0.71,0.66,0.61
  EndDataSection
I am accessing the data with this procedure:

Code: Select all

  ;- Global Variables
  Global NewList AgeFactor.i()
  Global NewList AgeExpectation.d()
  
  ;- Internal Procedures
  Procedure Init()
    
    Shared AgeFactor()
    Shared AgeExpectation()
    
    ClearList(AgeFactor())
    ClearList(AgeExpectation())
    
    Restore AgeTo
    
    For l = 0 To 10
      Read r5.d
      AddElement(AgeExpectation())
      AgeExpectation() = r5
    Next
    
    Restore NumberLives
    
    For l = 0 To 113
      Read r4.i
      AddElement(AgeFactor())
      AgeFactor() = r4
    Next
    
    ForEach AgeExpectation()
      Debug AgeExpectation()
    Next
    
  EndProcedure
The Debug results are horrendous and do not represent the the correct values for the Data.d part. The Data.i part works correctly and returns the correct values.

Can somebody advise me what I am doing wrong?

I am using PB5.6 in 64bit on MacOS.

Simon.
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: DataSection and Reading Data items issue

Post by Bisonte »

I think you have results like 76.150000000000006 ?

This is normal. you have to Debug or View your double variable with format, like
this

Code: Select all

ForEach AgeExpectation()
  Debug StrD(AgeExpectation(), 2)
Next
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: DataSection and Reading Data items issue

Post by STARGÅTE »

You have to specify the reading type:

Code: Select all

    
    For l = 0 To 10
      Read.d r5.d  ; <- Here!!
      AddElement(AgeExpectation())
      AgeExpectation() = r5
    Next

Code: Select all

    For l = 0 To 113
      Read.i r4.i
      AddElement(AgeFactor())
      AgeFactor() = r4
    Next
Edit: btw. you don't need Shared for AgeFactor() if you defined it as Global.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: DataSection and Reading Data items issue

Post by simberr »

The actual answer I get is:

Code: Select all

4635058996805998592.0
4635022405059026944.0
4634954851064615936.0
4634885889695322112.0
4634816928326028288.0
4634747263269292032.0
4634677598212555776.0
4634607933155820544.0
4634538268099084288.0
4634468603042348032.0
4634398234298170368.0
The code I posted was after a lot of experiments and I had the original as this:

Code: Select all

    Restore AgeTo
    
    For l = 0 To 10
      Read r5.d
      AddElement(AgeExpectation())
      AgeExpectation() = r5
    Next
    
    Restore NumberLives
    
    For l = 0 To 113
      Read r4.i
      AddElement(AgeFactor())
      AgeFactor() = r4
    Next
The issue I am having is that instead of 76.15 (or an approximation) I am getting 4635058996805998592.0!
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: DataSection and Reading Data items issue

Post by NicTheQuick »

Please read STARGÅTE's post again.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: DataSection and Reading Data items issue

Post by simberr »

I have read elsewhere on this forum (can't find it now) that there may be an issue with reading data in the 64bit version of PB.

Is that still true?
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: DataSection and Reading Data items issue

Post by simberr »

@NicTheQuick

Yes, I did. The post that I made with the results is after I changed it to

Code: Select all

    For l = 0 To 119
      Read r5.d
      AddElement(AgeExpectation())
      AgeExpectation() = r5
    Next
I have now changed it to:

Code: Select all

    Protected r4.i, r5.d
    ClearList(AgeFactor())
    ClearList(AgeExpectation())
    
    Restore AgeTo
    
    For l = 0 To 119
      Read r5
      AddElement(AgeExpectation())
      AgeExpectation() = r5
    Next
    
    Restore NumberLives
    
    For l = 0 To 113
      Read r4
      AddElement(AgeFactor())
      AgeFactor() = r4
    Next
Same result. Here is the first few lines of my output:

Code: Select all

4635058996805998592.00
4635022405059026944.00
4634954851064615936.00
4634885889695322112.00
4634816928326028288.00
4634747263269292032.00
4634677598212555776.00
4634607933155820544.00
4634538268099084288.00
4634468603042348032.00
4634398234298170368.00
This is using

Code: Select all

Debug StrD(AgeExpectation(), 2)
I am lost!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: DataSection and Reading Data items issue

Post by RASHAD »

@simberr
What STARGÅTE and NicTheQuick trying to tell you that you should use the data type with Read
Like Read.d rd5.d
Egypt my love
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: DataSection and Reading Data items issue

Post by simberr »

Ok, I have now solved the issue.

Now I have removed from all the data elements which are double the period ("."), converted them all to integers. When reading the item I divide the result by 100 which converts them back to their double originals.

My DataSection now looks like this:

Code: Select all

  DataSection
    NumberLives:
    Data.i 100000,99362,99317,99289,99266,99249,99234,99219,99206,99194,99184,99174,99164,99151,99131,99100,99059,99006,98941,98863,98771
    Data.i 98663,98542,98410,98272,98131,97989,97844,97698,97547,97393,97235,97072,96906,96736,96562,96383,96198,96006,95809,95603,95389
    Data.i 95164,94925,94671,94397,94102,93784,93436,93054,92632,92168,91659,91103,90501,89851,89150,88396,87588,86724,85802,84819,83772
    Data.i 82663,81498,80277,78995,77644,76216,74704,73100,71393,69574,67640,65592,63426,61130,58693,56117,53406,50564,47585,44475,41251
    Data.i 37939,34566,31158,27748,24374,21079,17915,14934,12186,9714,7549,5706,4193,2996,2083,1410,932,600,376,228
    Data.i 124,76,42,22,11,5,2,1,0,0
    AgeTo:
    Data.i 7615,7563,7467,7369,7271,7172,7073,6974,6875,6776
    Data.i 6676,6577,6478,6379,6280,6182,6084,5988,5891,5796
    Data.i 5701,5608,5514,5422,5329,5237,5144,5052,4959,4867
    Data.i 4775,4682,4590,4498,4406,4314,4222,4130,4038,3946
    Data.i 3854,3763,3672,3581,3490,3400,3311,3222,3134,3046
    Data.i 2960,2875,2790,2707,2625,2543,2463,2383,2305,2227
    Data.i 2151,2075,2000,1927,1853,1781,1709,1638,1568,1498
    Data.i 1430,1363,1297,1233,1170,1108,1048,989,933,877
    Data.i 824,772,723,675,630,587,545,506,469,435
    Data.i 403,373,346,321,299,280,263,248,234,222
    Data.i 211,200,189,179,169,159,150,141,133,125
    Data.i 117,110,103,096,089,083,077,071,066,061
  EndDataSection
And my reading code is now:

Code: Select all

    Restore AgeTo
    
    For l = 0 To 119
      Read r5.i
      AddElement(AgeExpectation())
      AgeExpectation() = r5 / 100
    Next
It works as expected.

I am still lost, however, as the Help file says that I can mix the data types within the DataSection and that does not appear to be true.

Can anybody throw any light on this?

Simon.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: DataSection and Reading Data items issue

Post by Trond »

Code: Select all

Define Lives.i
Restore NumberLives
For I = 0 To 7
  Read Lives
  Debug Lives
Next

Define Age.d ; <- d
Restore AgeTo
For I = 0 To 7
  Read.d Age
  Debug StrD(Age, 2)
Next



  DataSection
    NumberLives:
    Data.i 1, 2, 3, 4
    Data.i 5, 6, 7, 8
  EndDataSection
  
  DataSection
    AgeTo:
    Data.d 76.15, 75.63, 74.67, 73.69, 72.71
    Data.d 123.456, 876.543, 99.99
  EndDataSection
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: DataSection and Reading Data items issue

Post by Shardik »

simberr wrote:@NicTheQuick

Yes, I did.
Unfortunately you didn't read STARGÅTE's posting carefully enough. You tried

Code: Select all

Read r5.d
but STARGÅTE posted

Code: Select all

Read.d r5.d  ; <- Here!!
It's very important to use "Read.d r5.d" because otherwise Read defaults to Integer and this is causing all your troubles... :wink:
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: DataSection and Reading Data items issue

Post by skywalk »

This happens enough that the compiler should throw an error for naked Read's.

Code: Select all

Read.i  <-- Allowed
Read    <-- Error
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: DataSection and Reading Data items issue

Post by simberr »

Oh, goodness!

I did miss that - so sorry.

I have now refactored the code as doubles and I am getting the correct responses now.

As I said in my first post, I am learning PB and trial and erroring every day!

Thanks to you all I am slowly getting there. Very happy at the moment.

Thank you all, again, for your patience.

Simon.
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: DataSection and Reading Data items issue

Post by NicTheQuick »

Sometimes you just have not the right amount of caffeine in your bloodstream. Then it takes longer to understand. :mrgreen:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: DataSection and Reading Data items issue

Post by Lord »

NicTheQuick wrote:Sometimes you just have not the right amount of caffeine in your bloodstream. Then it takes longer to understand. :mrgreen:
Or stay awake all night long and then read the postings again.
Untested but might help... :wink:
Image
Post Reply