Results 1 to 9 of 9

Thread: [RESOLVED] Which variable type for math on duration value ?

  1. #1

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Resolved [RESOLVED] Which variable type for math on duration value ?

    Hi,

    On Jingle Palette, I'm adding the duration of each jingles.

    I get each duration from BASS Plugin :

    Code:
    Dim cHandle As String
    Dim JSec As Variant
    cHandle = BASS_StreamCreateFile(BASSFALSE, tpath, 0, 0, 0)
    JSec = BASS_ChannelBytes2Seconds(cHandle, BASS_StreamGetLength(cHandle))
    MsgBox JSec displays the jingle duration with 5 decimals with comma. For example 17,54832

    If before MsgBox, I do some math functions, to get a friendly value :
    JSec = Round(JSec + 0.05, 2)
    JSec = Fix(JSec * 10) / 10
    JSec = FormatNumber(JSec, 1)

    I get 17,6

    I store the 5 decimals value in palette.ini file (where are stored palettes and jingles infos like loop mode etc etc). For example, the first jingle duration of a palette :
    Duration_0=17,54832

    Then, Jingle Palette reads the ini file on loading a palette (at launch, or if I change another palette)
    Code:
    Dim DurationDisp As Variant
    DurationDisp = PalSet.Entry("Duration_" & Index, , Section) 'Reading from palette.ini file
    DurationDisp = Round(DurationDisp + 0.05, 2)
    DurationDisp = Fix(DurationDisp * 10) / 10
    DurationDisp = FormatNumber(DurationDisp, 1)
    I get a mismatch error since the first math operation ( DurationDisp = Round(DurationDisp + 0.05, 2) ).

    I replace ini file reading by a fixed value :
    Code:
    DurationDisp = "17,54832"
    I get no error and the math function are done.

    Same thinkg with:
    Code:
    DurationDisp = 17.54832
    I tried this :
    Code:
    DurationDisp = CStr(PalSet.Entry("Duration_" & Index, , Section))
    and this :
    Code:
    DurationDisp = CDec(PalSet.Entry("Duration_" & Index, , Section))
    But I still get error

    I tried to store duration with point instead of comma (with Replace function), but nithing better.

    I don't know where to search

    Some ideas ?

    Thanks !
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Which variable type for math on duration value ?

    You have posted this in a CodeBank forum, which are for sharing working code samples, not asking questions. I have asked the mods to move this thread to the appropriate forum.

  3. #3

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Which variable type for math on duration value ?

    Hi,

    Yes, sorry, I don't know how I was in the wrong section. Probably too tired (night work ^^ ).

    Thanks !
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Which variable type for math on duration value ?

    Moderator Actions: Moved to VB6 and Earlier
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Which variable type for math on duration value ?

    if you look in the locals windows between steps, you should be able to see what type the variant contains, then you can test to see what type will work for your calculation

    when i tested your code (without reading an ini file) the variant variable started as a string, changed to a double for the first 2 calculations then bask to a string to return the format
    as my decimal separator is period, the calculation was wrong in orders of magnitude 17,54832

    you might need to show the palset type and how it is read from file, and possibly a copy of the ini file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Which variable type for math on duration value ?

    I'm not a VB6 programmer, but why would you want to declare the variable as a variant to begin with? Isn't legacy Visual Basic designed to be a strongly typed language? If it is, then why not declare multiple variables as their expected type:
    Code:
    ' read from an *.ini file
    Dim durationFromIni As String
    durationFromIni = PalSet.Entry("Duration_" & Index, , Section)
    
    ' massage the data using numeric operations
    Dim massagedDuration As Double
    massagedDuration = Round(CDbl(Val(durationFromIni)) + 0.05, 2)
    massagedDuration = Fix(massagedDuration * 10) / 10
    
    ' format the number back to a String
    Dim formatedDuration As String
    formattedDuration = FormatNumber(massagedDuration, 1)
    Now from a purely developer and language agnostic point of view, if you setup a breakpoint then you can step through the code and see why you're getting the error.

    If I had to guess, then I think it is probably the implicit conversion from a String to a Double when you use Round.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    373

    Re: Which variable type for math on duration value ?

    I think the variable Type is Rational. For example, if your JPEG file has EXIF information which includes GPS data, e.g. the ones relating to Latitude and Longitude - they are Rational. This type of variable has 3 sections (Rationals), each is of 8-byte long for Degree, Minute and Second. When you call Google Map to show the image of the spot where the photo was taken, you pass the GPS information and you might have to convert it to a decimal (with many digits after the decimal point).
    Last edited by Brenker; Jan 12th, 2022 at 07:09 PM.

  8. #8

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Which variable type for math on duration value ?

    Hi friends,

    I was no longer receive notification about this thread (perhaps since moved ? ).

    thanks for your answers

    I digged on the web to see if there was a way to see the type of a variable, which is possible with VarType .

    So, directly fromm BASS Audio, the data was type 4 (Simple, if I follow the table of types I found), and from the ini file, it was 8 (String).

    CDbl helped me to resolve the problem and it matches with your ideas (if I understood them well).

    See ya
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  9. #9
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    373

    Re: Which variable type for math on duration value ?

    I think there is a difference between Rational and Double. A Rational is 8-byte long, but it is actually formed of two parts within itself: "the first integer is the numerator and the second integer is the denominator" using Microsoft's words. For example. in the case of "MM" in "time" HH:MM:SS, or in "latitude" Degree:MM:SS, "MM" contains "minutes" (numerator) and "60" (denominator).

    Rational type variable appears very often in TIFF and JPEG files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width