Error converting `big.Rat` to `float`

Hello.

With v0.123 update I’m getting the following error while generating a site:

error calling partial: "…/layouts/partials/figure.html:21:21": execute of template failed: template: partials/figure.html:21:21: executing "partials/figure.html" at <float .>: error calling float: unable to cast big.Rat{a:big.Int{neg:false, abs:big.nat{0x3}}, b:big.Int{neg:false, abs:big.nat{0xa}}} of type big.Rat to float64

What I’m doing is parsing an image EXIF data:

{{ with .file }}
…
	{{ with .Exif }}
…
		{{ with .Tags.ExposureTime }}
			{{ $exposure := 0 }}
			{{ if ( hasPrefix . "1/" ) }}
				{{ $exposure = ( div 1 ( float ( strings.TrimPrefix "1/" . ) ) ) }}
			{{ else }}
				{{ $exposure = ( float . ) }}
			{{ end }}
…

The error happens on the {{ $exposure = ( float . ) }} line.

I’m not sure why this error happens and why a rational number, 0.3 in this case, cannot be converted into a floating point number. With v0.122 it worked just fine.

Any ideas please?

Thank you.

Can you upload (do not cut/paste) an image that’s triggering this?

Within the folder that triggers this failure I see 3 images with an exposure of 0.3:

https://natalenko.name/myfiles/images.zip

Does this help?

Yes, it helps. The problem is with these images, not the Hugo version. I get the same error with DSC_0171.JPG when running v0.122.0.

Running exiftool:

Exposure Time : 0.3

There’s no way that ratio begins with a 1, unless its 1/3.3333333333333333. Maybe the ratio is 3/10, in which case float ain’t gonna work.

The ratio doesn’t begin with 1, the code directly tries to convert 0.3 to float. Why this is not going to work? I don’t understand your answer, sorry. I have these very pictures converted with existing code in v0.122 just fine, the result is visible here: Львів | post-factum's notes

With your code:

{{ with resources.Get "DSC_0171.JPG" }}
  {{ with .Exif }}
    {{ with .Tags.ExposureTime }}
      {{ $exposure := 0 }}
      {{ if ( hasPrefix . "1/" ) }}
        {{ $exposure = ( div 1 ( float ( strings.TrimPrefix "1/" . ) ) ) }}
      {{ else }}
        {{ $exposure = ( float . ) }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}
Start building sites … 
hugo v0.122.0-b9a03bd59d5f71a529acb3e33f995e0ef332b3aa+extended linux/amd64 BuildDate=2024-01-26T15:54:24Z VendorInfo=gohugoio

Total in 28 ms
Error: error building site: render: failed to render pages: render of "home" failed: "/home/jmooring/code/hugo-testing/layouts/_default/home.html:24:25": execute of template failed: template: _default/home.html:24:25: executing "main" at <float .>: error calling float: unable to cast big.Rat{a:big.Int{neg:false, abs:big.nat{0x3}}, b:big.Int{neg:false, abs:big.nat{0xa}}} of type big.Rat to float64

Notice the version. This isn’t anything new.

No, it does not.

Try this:

{{ with resources.Get "DSC_0171.JPG" }}
  {{ with .Exif }}
    {{ with .Tags.ExposureTime }}
      {{ . | string | warnf "%#[2]v (%[2]T) [%[1]d]" math.Counter}}
    {{ end }}
  {{ end }}
{{ end }}

What do you get in the console?

“3/10” (string)

See what I mean?

OK, I get the same error too with older versions after wiping the generated resources.

Any idea how to approach this conversion correctly then?

Why not just display the ratio? An exposure time of 1/3 is accurate. An exposure time of 0.333333 is only sort-of accurate.

I’d like to have the exposure converted to float to display it differently later depending on whether it is greater than 1 second or it is less than 1 second.

Then you’re going to have to test for data type, look for a /, split, divide.

OK, I’ve ended up with this ATM:

		{{ with .Tags.ExposureTime }}
			{{ $exposure := 0 }}
			{{ if ( strings.Contains . "/" ) }}
				{{ $fraction := ( split . "/" ) }}
				{{ $a := ( float ( index $fraction 0 ) ) }}
				{{ $b := ( float ( index $fraction 1 ) ) }}
				{{ $exposure = ( div $a $b ) }}
			{{ else }}
				{{ $exposure = ( float . ) }}
			{{ end }}
			{{ if ( ge $exposure 1 ) }}
				{{ $title = ( print $title " | " $exposure " s") }}
			{{ else }}
				{{ $title = ( print $title " | 1/" ( math.Round ( div 1 $exposure ) ) " s") }}
			{{ end }}
		{{ end }}

This does seem to work, the build succeeds, although I haven’t checked the correctness of the result with all the images.

Thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.